我的UIView
上有两个按钮。我希望这两个按钮之间的水平距离应该是superview的20%宽度?
对此有何帮助?
答案 0 :(得分:0)
您可以使用两个按钮之间的空虚拟视图来完成此操作,这两个按钮的宽度是超视图宽度的20%。
在代码中,它看起来像这样:
// The important thing here is that the buttons are flush against the spacer
[superview addConstraints:
[NSLayoutConstraint
constraintsWithVisualFormat:@"[button1]-0-[spacer]-0-[button2]"
options:0
metrics:nil
views:@{@"button1" : button1, @"button2" : button2, @"spacer" : spacer}]];
// Here, we set the width of the spacer to 20% of the super view
[superview addConstraint:
[NSLayoutConstraint
constraintWithItem:spacer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeWidth
multiplier:0.2
constant:0]];