简体   繁体   English

以编程方式更改Y位置约束

[英]Changing Y position constraints programmatically

在此处输入图片说明
This is four button open new view between them. 这是四个按钮之间打开的新视图。

I set constraints through Xcode, and want to change position programatically, but it does not work. 我通过Xcode设置了约束,并且想以编程方式更改位置,但是它不起作用。 I am new, kindly help. 我是新来的,请帮忙。 I used 我用了

[_btn_patientInformation.translatesAutoresizingMaskIntoConstraints: NO];
NSLayoutConstraint *constraintCoordinatesCenterY =[NSLayoutConstraint                                                  constraintWithItem:_btn_patientInformation attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:coordinatesView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:1];

But unfortunately it does not work. 但不幸的是,它不起作用。

Use outlet for the constraint you want to change. 将出口用于要更改的约束。

The outlet will be looking like this: 出口将如下所示:

 @property (weak, nonatomic) IBOutlet NSLayoutConstraint * topPositionConstraint;

Connect the above constraint to the Y constraint in storyboard. 将以上约束连接到情节提要中的Y约束。

And to change the constraint, you can simply do: 要更改约束,您可以简单地执行以下操作:

  topPositionConstraint.constant=yourValue;

and dont forget to call 别忘了打电话

    [self.view layoutIfNeeded];

after the change in constraints. 改变约束之后。

Try to set the constraints this way: 尝试通过以下方式设置约束:

  • Select the top button, and give leading,trailing,top,height,width constraint. 选择顶部按钮,并给出前导,尾随,顶部,高度,宽度约束。

在此处输入图片说明

  • Now select the second button, and ctrl drag it to first button, and release the mouse click, and select vertical spacing and leading constraint. 现在选择第二个按钮,然后按住Ctrl并将其拖到第一个按钮,然后释放鼠标单击,然后选择垂直间距和前导约束。

在此处输入图片说明 在此处输入图片说明

  • Do the same for the third button,except you ctrl drag the third button to the second button. 对第三个按钮执行相同的操作,除非您将第三个按钮拖动到第二个按钮。

在此处输入图片说明

Now to move all these buttons up or down , you can simply change the top constraint for the first button. 现在,要上下移动所有这些按钮,您只需更改第一个按钮的顶部约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM