![](/img/trans.png)
[英]How to Programatically Change Position of UIViews When Autolayout is On
[英]How do I programatically assign a UIViews Autolayout edges to match the superview?
我有一个要添加到ViewController的模式视图,现在传统上我总是只根据屏幕大小调整大小,以便模式的背景可以覆盖整个屏幕,并具有一个较小的alpha值,以便可以看到内容。
使用我当前的应用程序,我允许用户更改方向,因此上述方法不起作用。 我试图找到一种以编程方式将与self.view相关的模式的所有自动布局边分配为0的方法。 我尝试了以下代码,但出现错误。
[self.view addSubview:self.viewPromptSignup];
self.viewPromptSignup.translatesAutoresizingMaskIntoConstraints = NO;
/* pin Left of child to left of parent */
[self.viewPromptSignup addConstraint:[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.viewPromptSignup
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0]];
/* pin Right of child to right of parent */
[self.viewPromptSignup addConstraint:[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.viewPromptSignup
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];
/* pin top of child to bottom of nav bar(or status bar if no nav bar) */
[self.viewPromptSignup addConstraint:[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.viewPromptSignup
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
/* pin Top of nav bar to bottom of child view */
[self.viewPromptSignup addConstraint:[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.viewPromptSignup
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
//Creating View
UIView *viewPromptSignup=[UIView new];
viewPromptSignup.translatesAutoresizingMaskIntoConstraints = NO;
[viewPromptSignup setBackgroundColor:[UIColor greenColor]];
//adding to Parent View
[self.view addSubview:viewPromptSignup];
//Top and Bottom Guide
id topGuide = self.topLayoutGuide;
id bottomGuide = self.bottomLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (viewPromptSignup, topGuide,bottomGuide);
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-10-[viewPromptSignup]"
options: 0
metrics: nil
views: viewsDictionary]
];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat: @"V:[viewPromptSignup]-10-[bottomGuide]"
options: 0
metrics: nil
views: viewsDictionary]
];
// align viewPromptSignup from the left and right
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[viewPromptSignup]-0-|" options:0 metrics:nil views:viewsDictionary]];
您可以使用Masonry库,因为添加约束非常容易。
检查一下https://github.com/SnapKit/Masonry
使用此库,您可以像这样轻松设置约束:
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top);
make.left.equalTo(superview.mas_left);
make.bottom.equalTo(superview.mas_bottom);
make.right.equalTo(superview.mas_right);
}];
超级容易
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.