Is there a way to add an identifier to Auto Layout Constraints in Interface Builder?(有没有办法在 Interface Builder 中向 Auto Layout Constraints 添加标识符?)
问题描述
在 Interface Builder 中进行了一些可视化布局之后,我创建了一些我想在运行时访问的约束.有没有办法在 Interface Builder 中标记或识别约束,以便以后查找它们?
After doing some visual layout in Interface Builder I've created some constraints that I want to access at runtime. Is there a way to label or identify constraints in Interface Builder so they can be looked-up later?
我想这样做的原因是我需要根据视觉上指定的约束执行一些计算.我知道 Apple 提供了 视觉格式语言,我可以在控制器中以编程方式指定约束.我宁愿不使用这种方法,以免丢失 IB 的设计时反馈.
The reason I want to do this is I need to perform some calculation base upon the constraints that are visually specified. I am aware that Apple has provided the Visual Format Language and I could specify the constraints programmatically in a controller. I rather not use this approach so I don't lose the design time feedback of IB.
制作参考插座确实有效,但问题仍然存在.
Making a referencing outlet did work, but the question still stands.
推荐答案
更新:
正如 Bartłomiej Semańczyk 在他的回答中所解释的,现在有一个可见的标识符字段在 NSLayoutConstraint
的 Attributes Inspector 中,无需自己公开此字段.只需在 Document Outline 视图或 Storyboard 视图中选择约束,然后在右侧的 Attributes Inspector 中添加一个标识符.
As explained by Bartłomiej Semańczyk in his answer, there is now an Identifier field visible in the Attributes Inspector for the NSLayoutConstraint
making it unnecessary to expose this field yourself. Just select the constraint in the Document Outline view or in the Storyboard view and then add an identifier in the Attributes Inspector on the right.
较早的答案:
是的,这可以做到.NSLayoutConstraint
有一个名为 identifier
的属性,可以在 Interface Builder 中公开和分配.为了演示这个,我创建了一个单视图应用程序,它有一个红色框的子视图.这个子视图有 4 个约束:宽度、高度、在容器中水平居中、在容器中垂直居中.我通过执行以下操作为宽度约束提供了标识符 redBoxWidth
:
Yes, this can be done. NSLayoutConstraint
has a property called identifier
than can be exposed in Interface Builder and assigned. To demo this, I created a Single View Application that has a single subview that is a red box. This subview has 4 constraints: width, height, centered horizontally in container, centered vertically in container. I gave the width constraint the identifier redBoxWidth
by doing the following:
单击文档布局视图中的宽度限制.然后在Identity Inspector中User Defined Runtime Attributes下,点击Key Path下的
+
.将 keyPath 更改为 identifier,将 Type Boolean 更改为 String,并设置值到redBoxWidth
.
Click on the width constraint in the Document Layout View. Then in the Identity Inspector under User Defined Runtime Attributes, click on the
+
under Key Path. Change keyPath to identifier, change the Type Boolean to String, and set the Value toredBoxWidth
.
那么在ViewDidLoad
中就可以通过名字找到约束并改变它的值:
Then in ViewDidLoad
it is possible to find the constraint by name and change its value:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
for subview in view.subviews as [UIView] {
for constraint in subview.constraints() as [NSLayoutConstraint] {
if constraint.identifier == "redBoxWidth" {
constraint.constant = 300
}
}
}
}
}
这篇关于有没有办法在 Interface Builder 中向 Auto Layout Constraints 添加标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在 Interface Builder 中向 Auto Layout Constraints 添加标识符?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01