有没有办法在 Interface Builder 中向 Auto Layout Constraints 添加标识符?

Is there a way to add an identifier to Auto Layout Constraints in Interface Builder?(有没有办法在 Interface Builder 中向 Auto Layout Constraints 添加标识符?)

本文介绍了有没有办法在 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 在他的回答中所解释的,现在有一个可见的标识符字段在 NSLayoutConstraintAttributes 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:

  1. 单击文档布局视图中的宽度限制.然后在Identity InspectorUser Defined Runtime Attributes下,点击Key Path下的+.将 keyPath 更改为 identifier,将 Type Boolean 更改为 String,并设置redBoxWidth.

  1. 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 to redBoxWidth.

那么在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 添加标识符?