Skip to content

Instantly share code, notes, and snippets.

@reyandrey
Created July 7, 2019 18:57
Show Gist options
  • Save reyandrey/48cfb04dcc17d19d8d2345a52b8859ea to your computer and use it in GitHub Desktop.
Save reyandrey/48cfb04dcc17d19d8d2345a52b8859ea to your computer and use it in GitHub Desktop.
View from XIB Swift Example
import UIKit
import Foundation
@IBDesignable
class ControlPanelView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViews()
}
}
extension ControlPanelView {
private func loadFromXib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "ControlPanelView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
private func setupViews() {
let viewFromXib = loadFromXib()
viewFromXib.frame = self.bounds
viewFromXib.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(viewFromXib)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment