![](/img/trans.png)
[英]How to test a UILabel on a programatically added UIView with XCTest
[英]How to update constraint of a UILabel programatically
我在stroyboard中创建了一个UILabel
,其框架是(15, 55, 250, 20)
15,55,250,20 (15, 55, 250, 20)
。 根据我的要求,我想通过编码将标签框架更改为(15, 10, 250, 20)
。 但是对该标签没有任何影响。
所以我通过以下编码更新约束:
mylabel.frame = CGRectMake(15, 10, 250, 20);
[mylabel needsUpdateConstraints];
但它不起作用。 请有人帮助我。 我在最近2天坚持这个。
我通过故事板添加这些约束
使用AutoLayout更新帧不起作用。 相反,创建约束并设置动画。
以下是如何创建NSLayoutConstraint
并将其添加到视图中的方法:
var myLabelConstraint = NSLayoutConstraint (item: myLabel,
attribute: NSLayoutAttribute.Top, // You can also choose Width, Height, Bottom, Leading, Trailing
relatedBy: NSLayoutRelation.Equal,
toItem: self.view
attribute: NSLayoutAttribute.Top,
multiplier: 1,
constant: 10)
self.view.addConstraint(myLabelConstraint)
如果要为宽度和高度创建约束,请记住将toItem
更改为nil
并将attribute
更改为NSLayoutAttribute.NotAnAttribute
。
如果要为约束设置动画,只需更改它的constant
:
myConstraint.constant = 15
self.view.layoutIfNeeded()
首先,您应该将一个约束删除到屏幕的右边缘(45); 你过度约束你的观点了。
当您想要更改在故事板中创建的约束时,您应该为它创建一个IBOutlet,并在代码中修改其常量值。 由于你想要改变y位置,建立一个垂直间距约束(10个)的出口,我将这个例子称为yCon。 你只需要这样做,
self.yCon.constant = 55;
[self.view layoutIfNeeded];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.