如何在自动布局中使用约束标识符,以及如何使用标识符更改约束? [迅速] [英] How to use constraint Identifiers in autolayout and how to change constrain using identifiers? [Swift]

查看:73
本文介绍了如何在自动布局中使用约束标识符,以及如何使用标识符更改约束? [迅速]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Xcode 7中编辑约束时,我在Interface Builder中找到了 identifier 字段.约束的标识符是什么?如何使用约束?使用标识符,我可以通过编程方式访问约束并更改常量吗?我的问题是,标识符为什么有用,又有什么用?

While I was editing my constraint in Xcode 7 I found identifier field in Interface Builder. What is an identifier for the constraint how do I use it? using identifier can I access constraint programmatically and change the constant? my question is why and how is that identifier is helpful?

有什么方法可以通过在子视图中循环并再次在约束子视图中循环来使用标识符访问约束.我的意思是有什么方法可以直接访问约束而无需循环.

Is there any way to access the constraint with an identifier with looping in subviews and again looping constrain subviews. I mean is there any way we can access constraint directly without a loop.

更新

我尝试了此代码,但仅访问了宽度和高度约束

I Tryed this code but only width and height constrain is accessed

    for subview in view.subviews {
        for constraint in subview.constraints() {
           if constraint.identifier == "identifier" {
                return constraint
           }
        }
    }

推荐答案

HOW:标识符在调试时很有用(例如,约束不匹配且其中之一被破坏了)在运行时;约束标识符将显示在日志中,以便您查看哪个可能导致问题)

HOW: The identifier is useful when debugging (e.g. constraints not matching and one of them gets broken at run time; the constraint identifiers are being shown in log so you can see which one might cause problems)

原因::约束标识符使日志更易于阅读,更准确,并且为您节省了很多时间.

WHY: Constraint identifiers make logs easier to read, more accurate and they save you a lot of time.

约束:如果要以编程方式更改约束,则必须将其声明为出口(如标签或按钮),然后从视图中将其删除(不是对象本身),然后将它们再次设置为视图.据我所知,您不能只是以编程方式编辑"约束.

Constraint editing: If you want to change constraints programmatically you will have to declare them as outlets (like a label or button), then remove them from the view (NOT the object itself) and then set them again to the view. From my knowledge you can't just "edit" the constraints programmatically.

您的代码仅提供宽度和高度,因为您访问的视图约束仅包含对象的宽度和高度.

Your code gives you only width and height because you access the view's constraints which only contains the objects' widths and heights.

这篇关于如何在自动布局中使用约束标识符,以及如何使用标识符更改约束? [迅速]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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