您可以使用类别/扩展名向UIView添加IBDesignable属性吗? [英] Can you add IBDesignable properties to UIView using categories/extensions?

查看:95
本文介绍了您可以使用类别/扩展名向UIView添加IBDesignable属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些不知道我在说什么的人,Xcode 6.0添加了新功能,IBDesignable和IBInspectable。

For those that don't know what I'm talking about, Xcode 6.0 added new features, IBDesignable and IBInspectable.

使用IBInspectable标记自定义视图时属性,这些属性显示在IB的Attributes Inspector中。

When you tag your custom views with IBInspectable properties, those properties show up in the Attributes Inspector in IB.

同样,当您使用IBDesignable标记自定义UIView子类时,Xcode会编译您的视图并调用代码以在Xcode窗口中呈现您的视图对象,这样您就可以看到它们看起来像。

Likewise, when you tag a custom UIView subclass with IBDesignable, Xcode compiles your views and invokes the code to render your view objects right in the Xcode window so you can see what they look like.

在Swift和Objective-C中,将IBDesignable和IBInspectable属性添加到自定义视图的技术几乎完全相同。 IBInspectable属性出现在Interface Builder Attributes Inspector中,无论您使用哪种语言来定义它们。

The technique for adding IBDesignable and IBInspectable attributes to custom views is pretty much identical in Swift and Objective-C. IBInspectable properties appear in the Interface Builder Attributes Inspector regardless of which language you use to define them.

我在Objective-C中创建了一个UIView类别,并且Swift中的UIView,它将视图底层的borderWidth,cornerRadius,borderColor和layerBackgroundColor属性作为视图的属性进行提升。如果更改属性,则扩展名/类别会根据需要键入转换,并将更改转发给图层。

I've created a category of UIView in Objective-C and an extension of UIView in Swift that promote the borderWidth, cornerRadius, borderColor, and layerBackgroundColor properties of the view's underlying layer as properties of the view. If you change the property, the extension/category does type conversion as required and forwards the change to the layer.

IBInspectable部分效果很好。我看到并且可以在IB属性检查器中设置新属性。

The IBInspectable part works great. I see and can set the new properties in the IB attributes inspector.

我上周可能发誓,我的视图类别/扩展名上的IBDesignable属性也正常工作,我可以在IB中看到我的自定义UIView类别渲染,它具有更改的图层属性。本周它无法正常工作。

I could have sworn that last week, the IBDesignable attribute on my view category/extension was working too, and I could see my custom UIView category rendering in IB with it's changed layer attributes. This week it isn't working.

我是否产生幻觉?

现有系统类的类别/扩展可以在使用IBDesignable设置时在Interface Builder中绘制自定义UI吗?

Can categories/extensions of existing system classes draw their custom UI in Interface Builder when they are set up with IBDesignable?

推荐答案

自发布此问题以来,我了解到@IBDesignable不适用于类扩展。您可以添加标记,但它不起作用。

Since posting this question I've learned that @IBDesignable does not work for class extensions. You can add the tag, but it has no effect.

这篇关于您可以使用类别/扩展名向UIView添加IBDesignable属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆