使用代码实现autolayout
2016-10-20 本文已影响23人
_君莫笑_
//|:表示父视图
//_:表示的是距离
//v:表示垂直方向
//H:表示水平方向
//>=:表示视图间距,宽度和高度必须大于或等于某个值
//<=:表示视图距离,宽度和高度必须小于或等于某个值
//==:表示视图间距,宽度或高度必须等于某个值
UIButton*button1 = [UIButtonbuttonWithType:UIButtonTypeSystem];
button1.backgroundColor= [UIColoryellowColor];
[button1setTitle:@"Click"forState:UIControlStateNormal];
[button1setTranslatesAutoresizingMaskIntoConstraints:NO];//标记是否自动布局
[self.viewaddSubview:button1];
UIButton*button2 = [UIButtonbuttonWithType:UIButtonTypeSystem];
[button2setBackgroundColor:[UIColorredColor]];
[button2setTitle:@"Click Me"forState:UIControlStateNormal];
[self.viewaddSubview:button2];
NSDictionary*views =NSDictionaryOfVariableBindings(button1, button2);
[self.viewaddConstraints:[NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-(==50)-[button1(100)]"options:0metrics:nilviews:views]];