问题描述
在故事板中有一个UIView,约束例如0.1比例高度。
In storyboard have a UIView, with a constraint for example "0.1" proportional height.
想象一下你有一个类似的约束0.1,在很多不同的视图上场景。
Imagine you have a similar constraint "0.1", on views in many different scenes.
假设您想要将值0.1更改为0.975。
Say you want to change the value 0.1 to 0.975.
您必须在每个地方更改它。
You have to change it individually everywhere.
是否有某种方法可以使用@IBInspectable和/或约束来制作一种全局值?
Is there some way to make a sort of "global value", using @IBInspectable and/or constraints?
这样您就可以一次性更改所有内容,并在故事板中一次性查看结果。
So that you can change them all at once, and see the results all at once in storyboard.
(当然,在运行时你可以在代码中传播它们。但最好在故事板上看到它。)
请注意,例如Wain的解决方案可以完美地运行:您可以设置一次值,并影响应用程序中的任何位置。但不幸的是,它只在运行时才这样做,你没有在故事板上看到它。
Note for example that Wain's solution below works perfectly: you can set the value once, and affect everywhere in the app. But unfortunately it only does that at run time, you don't see it on storyboard.
推荐答案
我还没试过,这只是输入这里的借口错别字,但我会考虑在 NSLayoutConstraint
上创建一个扩展名,类似于:
I haven't tried it, and this is just typed in here so excuse typos, but I would consider creating an extension on NSLayoutConstraint
, something along the lines of:
let constants = [
"bannerHeight" : CGFloat(50)
]
extension NSLayoutConstraint {
func setCommonConstant(name: String) {
self.constant = constants[name]!
}
}
然后在Xcode中使用用户定义的运行时属性指定要使用的名称:
and then using the user defined runtime attributes in Xcode so specify the name to use:
commonConstant, type String, value name
所以,像这样,在故事板中的任何位置的每个约束下:
So, like this, on each Constraint anywhere in the storyboard:
这篇关于某种“全球”的对于IBDesignables?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!