通过IBInspectable属性实时渲染IBOutlet连接子视图 [英] Live Render IBOutlet Connected Subviews Via IBInspectable Properties
问题描述
我在iOS 8中第一次使用故事板,到目前为止一直喜欢故事板上的实时渲染方面。但是,我似乎遇到了让我的观点在故事板上正确呈现的障碍。
I'm using storyboards for the first time in iOS 8 and so far have been loving the live rendering aspect of things on the storyboard. However, I seem to have hit a snag in getting my views to render properly on the storyboard.
我有一个容器 UIView
包含与故事板上的 UILabel
的连接,我试图根据 IBInspectable $ c $设置标签的文本c>标签的父容器视图上的属性。
I have a container UIView
that contains a connection to a UILabel
on the storyboard, I am attempting to set the label's text based on an IBInspectable
attribute on the label's parent container view.
@IBDesignable class ContainerView : UIView {
@IBOutlet weak var : titleLabel : UILabel!
@IBInspectable var title : String = "" {
didSet {
titleLabel?.text = title
}
}
/* Init functions */
prepareForInterfaceBuilder() {
self.titleLabel?.text = title
}
}
如果我在故事板中设置了属性,它会在程序执行时按预期呈现,但无法像我期望的那样在故事板中呈现。我已经检查了我的连接,所有内容似乎都已正确连接。
If I set the attribute in the storyboard it renders as expected while the program is executing but fails to render in the storyboard as I would expect. I've checked my connections and everything appears to be hooked up properly.
我的问题是:是否可以通过IBInspectable属性影响IBOutlet连接视图的内容让他们在故事板上现场渲染,如果是这样,我错过了什么或做错了什么?
My question is: Is it possible to affect the contents of an IBOutlet connected view via IBInspectable attributes and have them live render on the storyboard, and if so, what am I missing or doing wrong?
推荐答案
很遗憾,您无法在界面构建器中看到 IBOutlet
对象您的自定义视图,标记为 IBDesignable
。如果要在界面构建器中查看插座,则必须使用常规变量 IBOutlet
,并且必须以编程方式创建对象。
Unfortunately you can't see IBOutlet
objects in interface builder for your custom views which are marked as IBDesignable
. If you want to see your outlets in interface builder, you have to use regular variables instead IBOutlet
and you have to create your objects programmatically.
另请注意,如果您需要从对象的界面构建器更改某些内容,则必须将您的属性定义为 IBInspectable
。目前以下变量类型对 IBInspectable
有效:
Also please note that, if you need to change something from interface builder for your objects, you have to define your properties as IBInspectable
. Currently following variables types are valid for IBInspectable
:
Bool
, CGFloat
, CGPoint
, CGRect
, CGSize
, NSInteger
, NSString
, UIColor
, UIImage
我希望这个答案对您来说足够清楚。
I hope this answer is adequately clear for you.
编辑我发现以下文章描述了如何做你需要的方法:
I found following article which is describing a way how to do what you need:
http://justabeech.com/2014/07/27/xcode- 6-live-rendering-from-nib /
2nd Edit:我尝试了这篇文章,但它确实有效。现在我可以在界面构建器
2nd I tried the article and it works. Now I can see my outlets on interface builder
这篇关于通过IBInspectable属性实时渲染IBOutlet连接子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!