1

I've designed my custom view in xib. I have CustomView.h/m + CustomView.xib. In xib file I set view class to CustomView.

Now I want to use this view in other xib. I've created another xib, added view and set it class to CustomView, but it's not loaded.

How to load my CustomView correctly?

user1284151
  • 875
  • 4
  • 12
  • 23
  • Read this it will help you http://stackoverflow.com/questions/5095359/using-xib-object-inside-another-xib – Omarj Jul 01 '15 at 08:22
  • @Omarj provided answer provides infinite loop, you cannot call loadNib in initWithCode method – user1284151 Jul 01 '15 at 08:41
  • More precisely, how are you loading `CustomView` and what is exactly happening? Are you pushing a new `UIViewController`? – SwiftArchitect Jul 01 '15 at 08:46
  • @LancelotdelaMare currently, I've just added in other xib view and set its class to `CustomView`. Nothing happens, customview isn't loaded. I guess I need to write some code to make it appear in UI – user1284151 Jul 01 '15 at 08:49
  • If so, present question is a duplicate of http://stackoverflow.com/questions/15405916/correct-way-to-load-a-nib-for-a-uiview-subclass – SwiftArchitect Jul 01 '15 at 08:52
  • @LancelotdelaMare in that question view is loaded from code, not from xib/storyboard – user1284151 Jul 01 '15 at 08:55

2 Answers2

2

Short answer: Yes.

You can use CustomView.h/m in as many .XIB files as you see fit, including .storyboard

The problem lies in how you load the second view.

    if let navigationController = self.navigationController {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        if let vc = sb.instantiateViewControllerWithIdentifier("id") as? UIViewController {
            navigationController.pushViewController(vc, animated: true)
        }
    }

See:

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
1

You need to use the IB_Designable keyword to make your custom view look properly in IB. But designing the custom view in a nib and not overriding drawRect makes it more involved. Check out this link to see how you can do it:

Cihan Tek
  • 5,349
  • 3
  • 22
  • 29