我需要创建一个UISlider并将其放在现有滑块上方。
如果我想将视图附加到超级视图,我知道如何为视图创建约束:
UIView *superview = view.superview;
[view setValue: [NSNumber numberWithBool: FALSE] forKey: @"translatesAutoresizingMaskIntoConstraints"];
NSLayoutConstraint *topConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
constraintWithItem: view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem: superview
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
NSArray *constraints = @[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint];
[superview addConstraints: constraints];
但是当我需要将两个子视图附加在一起时,同样的方法不起作用。说,我有view1作为superview的子视图。它是在不久前创建的。现在我需要另一个(view2)在superview中具有相同的定位。
像
这样的东西UIView *superview = view1.superview;
UIView *view2 = [[UIView alloc] init];
[superview addSubview: view2];
[view2 setValue: [NSNumber numberWithBool: FALSE] forKey: @"translatesAutoresizingMaskIntoConstraints"];
NSLayoutConstraint *topConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
constraintWithItem: view1
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem: view2
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
NSArray *constraints = @[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint];
[superview addConstraints: constraints];
打破一切。
答案 0 :(得分:0)
看起来你只是向后......
您希望将Optional
约束为 view2
:
view1