如何在 InterfaceBuilder 中显示自定义 UIView?
我似乎喜欢设计新的 UIViews
和 UIControls
来实现自己的 -drawRect:
方法。 这对我来说效果很好,尤其是在 Interface Builder 中使用 UIViews 进行组合时。
但是在 Interface Builder 中组合它们很烦人,因为它们只是显示为无聊的普通矩形。
如果视图能够像内置控件那样真正呈现自己,那就太好了。
有没有什么方法可以构建我的项目,以便 Interface Builder 呈现我的自定义视图?
I seem to enjoy designing new UIViews
and UIControls
that implement their own -drawRect:
method. This work well for me, especially when composed using UIViews
in Interface Builder.
But composing them in Interface Builder is annoying because they just show up as boring plain rectangles.
It would be great if the views would actually render themselves as the built-in controls do.
Is there any way I can structure my project so that Interface Builder will render my custom views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了做到这一点,您实际上必须为 Interface Builder 创建一个使用自定义类的插件。 创建并安装插件后,您将能够将类的实例(您的视图)拖放到另一个窗口/视图/任何其他控件上,就像任何其他控件一样。 要开始创建 IB 插件,请参阅 Interface Builder 插件编程指南。 另外,我建议您看一下 Aaron Hillegass 的书,Mac OS 的 Cocoa 编程X。 它不仅写得很好且易于理解,而且有一章介绍如何创建您自己的 IB Palette 控件。
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.
这可以通过使用
@IBDesignable
标记 UIView 子类来实现。 完成此操作后,您的自定义视图将在 Interface Builder 中呈现。 您甚至可以添加可通过将其标记为@IBInspectable
来配置的参数。 下面是一个 Swift 示例:关于 NSHipster 的文章提供了更多详细信息。
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:There's an article on NSHipster that provides more detail.