How do you display custom UIViews in InterfaceBuilder?

橙三吉。 提交于 2019-11-27 19:27:50
Jason Coco

In order to do this, you actually have to create a plug-in for Interface Builder that uses your custom class. Once you create and install your plug-in, you will be able to drag and drop instances of your class (your view) onto another window/view/whatever just like any other control. To get started with creating IB Plug-Ins, please see the Interface Builder Plug-In Programming Guide. Also, I recommend taking a look at Aaron Hillegass's book, Cocoa Programming for Mac OS X. As well as being very well written and easy to understand, it has a chapter on creating your own IB Palette controls.

This is achievable by marking your UIView subclass with @IBDesignable. Once you do this, your custom views will render themselves in Interface Builder. You can even add parameters that can be configured by marking them as @IBInspectable. Here's a Swift example:

@IBDesignable class customView: UIView {

    @IBInspectable var count: Int = 0

}

There's an article on NSHipster that provides more detail.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!