IOS自动布局:通过编程设置宽度约束 [英] ios auto-layout: Programatically set width constraint

查看:248
本文介绍了IOS自动布局:通过编程设置宽度约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的iOS应用程序。我加入了自动布局编程到2标签。

I am working on an ios application. I am adding the auto-layout programatically to 2 labels.

我需要添加一个约束,使他们平等的宽度。

I need to add a constraint to make them equal width.

我知道如何使用固定标签宽度:

I know how to fix the width of a label by using :

constraint = [NSLayoutConstraint
    constraintWithItem:myLabel
             attribute:NSLayoutAttributeWidth
            relatedBy:NSLayoutRelationEqual
              toItem: nil
           attribute:NSLayoutAttributeNotAnAttribute
          multiplier:1.0f
            constant:200.0f];

这将修复标签大小设置为常。但是,我有2个标签,我希望他们能有平等的大小,而无需设置一个常量。

That would fix the label size to a constant. But I have 2 labels and I want them to have equal size without having to set a constant.

感谢

推荐答案

原来,我只是要做到以下几点:

It turned out I just have to do the following:

constraint = [NSLayoutConstraint
    constraintWithItem:myLabel
        attribute:NSLayoutAttributeWidth
        relatedBy:NSLayoutRelationEqual
          toItem: otherLabel
       attribute:NSLayoutAttributeWidth
      multiplier:1.0f
        constant:0];

这篇关于IOS自动布局:通过编程设置宽度约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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