How to embed a custom view xib in a storyboard scene?

前端 未结 11 646
生来不讨喜
生来不讨喜 2021-01-29 21:33

I\'m relatively new in the XCode/iOS world; I\'ve done some decent sized storyboard based apps, but I didn\'t ever cut me teeth on the whole nib/xib thing. I want to use the sam

11条回答
  •  长发绾君心
    2021-01-29 22:12

    A little bit more swifty version of @brandonscript 's idea with early return:

    override func awakeFromNib() {
    
        guard let xibName = xibName,
              let xib = Bundle.main.loadNibNamed(xibName, owner: self, options: nil),
              let views = xib as? [UIView] else {
            return
        }
    
        if views.count > 0 {
            self.addSubview(views[0])
        }
    
    }
    

提交回复
热议问题