Tag: UIKit/カスタムビュー
@IBDesignable class CustomView: UIView { override init(frame: CGRect) { super.init(frame: frame) loadFromNib() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) loadFromNib() } private func loadFromNib() { // ここは UINib を使っても良い // let v = NSBundle(forClass: self.dynami).loadNibNamed("CustomView", owner: self, options: nil).first as! UIView let v = UINib(nibName: "CustomView", bundle: Bundle(for: CustomView.self)).instantiate(withOwner: nil, options: nil)[0] as! UIView v.frame = self.bounds addSubview(v) } // ここでStoryboard上の見た目を初期化する override func prepareForInterfaceBuilder() { // <= (4) loadFromNib() } }