Created
July 7, 2019 18:57
-
-
Save reyandrey/48cfb04dcc17d19d8d2345a52b8859ea to your computer and use it in GitHub Desktop.
View from XIB Swift Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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