ios auto-layout: Programmatically set width constraint

♀尐吖头ヾ 提交于 2019-12-19 05:08:08

问题


I am working on an ios application. I am adding the auto-layout programmatically 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];

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.


回答1:


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];


来源:https://stackoverflow.com/questions/17317495/ios-auto-layout-programmatically-set-width-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!