如何在 InterfaceBuilder 中显示自定义 UIView?

发布于 2024-07-13 03:09:01 字数 306 浏览 8 评论 0原文

我似乎喜欢设计新的 UIViewsUIControls 来实现自己的 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-07-20 03:09:01

为了做到这一点,您实际上必须为 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.

葵雨 2024-07-20 03:09:01

这可以通过使用 @IBDesignable 标记 UIView 子类来实现。 完成此操作后,您的自定义视图将在 Interface Builder 中呈现。 您甚至可以添加可通过将其标记为 @IBInspectable 来配置的参数。 下面是一个 Swift 示例:

@IBDesignable class customView: UIView {

    @IBInspectable var count: Int = 0

}

关于 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:

@IBDesignable class customView: UIView {

    @IBInspectable var count: Int = 0

}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文