Search code examples
iosswiftuinavigationitemibdesignable

Make UINavigationItem as @IBDesignable


I would like to make custom UINavigationItem, and that changes could be made and seen in storyboard. I know how to make custom UIView with @IBDesignable and @IBInspectable, but my problem is that UINavigationItem does not have draw method and required init?(coder aDecoder: NSCoder) is called to soon.

How is the best approach to tackle this?


Solution

  • Unfortunately, it's not possible to make a custom UINavigationItem rendered in the interface builder (what @IBDesignable is meant for). In the docs (a Live Rendering section) they say you must inherit a class from UIView/NSView. In turn, UINavigationItem is inherited from NSObject.

    Live Rendering

    You can use two different attributes—@IBDesignable and @IBInspectable—to enable live, interactive custom view design in Interface Builder. When you create a custom view that inherits from the UIView class or the NSView class, you can add the @IBDesignable attribute just before the class declaration. After you add the custom view to Interface Builder (by setting the custom class of the view in the inspector pane), Interface Builder renders your view in the canvas.