以编程方式对子视图进行约束
Posted
技术标签:
【中文标题】以编程方式对子视图进行约束【英文标题】:Make constraints for subviews programmatically 【发布时间】:2017-07-25 12:49:19 【问题描述】:我需要创建一个 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];
破坏一切。
【问题讨论】:
让事情变得简单......分配slider.frame = oldSlider.frame 【参考方案1】:看起来你只是把它倒过来了......
你想限制view2
到 view1
:
[NSLayoutConstraint
constraintWithItem: view2 // <-- constrain this view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem: view1 // <-- to this view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
【讨论】:
以上是关于以编程方式对子视图进行约束的主要内容,如果未能解决你的问题,请参考以下文章