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

前端 未结 11 546
生来不讨喜
生来不讨喜 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:06

    Here's the answer you've wanted all along. You can just create your CustomView class, have the master instance of it in a xib with all the subviews and outlets. Then you can apply that class to any instances in your storyboards or other xibs.

    No need to fiddle with File's Owner, or connect outlets to a proxy or modify the xib in a peculiar way, or add an instance of your custom view as a subview of itself.

    Just do this:

    1. Import BFWControls framework
    2. Change your superclass from UIView to NibView (or from UITableViewCell to NibTableViewCell)

    That's it!

    It even works with IBDesignable to render your custom view (including the subviews from the xib) at design time in the storyboard.

    You can read more about it here: https://medium.com/build-an-app-like-lego/embed-a-xib-in-a-storyboard-953edf274155

    And you can get the open source BFWControls framework here: https://github.com/BareFeetWare/BFWControls

    And here's a simple extract of the code that drives it, in case you're curious: https://gist.github.com/barefeettom/f48f6569100415e0ef1fd530ca39f5b4

    Tom

提交回复
热议问题