简体   繁体   English

如何以编程方式分配UIViews自动布局边缘以匹配超级视图?

[英]How do I programatically assign a UIViews Autolayout edges to match the superview?

I have a modal view that I am adding to the ViewController, now traditionally I have always just resized based on the screen size so that the background of the modal covers the entire screen and has a slight alpha to allow the content to be seen through. 我有一个要添加到ViewController的模式视图,现在传统上我总是只根据屏幕大小调整大小,以便模式的背景可以覆盖整个屏幕,并具有一个较小的alpha值,以便可以看到内容。

With my current app I am allowing the user to change the orientation so the above method does not work. 使用我当前的应用程序,我允许用户更改方向,因此上述方法不起作用。 I was trying to find a way programmatically to assign all the autolayout edges of the modal to 0 in relation to self.view. 我试图找到一种以编程方式将与self.view相关的模式的所有自动布局边分配为0的方法。 I have tried the following code but am getting errors. 我尝试了以下代码,但出现错误。

[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]];

You can use Masonry library because it's super easy to add constraints. 您可以使用Masonry库,因为添加约束非常容易。

Check it out https://github.com/SnapKit/Masonry 检查一下https://github.com/SnapKit/Masonry

And using this library you can easily set the constraints like this: 使用此库,您可以像这样轻松设置约束:

[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);
}];

It's super easy 超级容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动布局打开时如何以编程方式更改UIViews的位置 - How to Programatically Change Position of UIViews When Autolayout is On 如何以编程方式折叠UIViews? - How do I collapse UIViews programatically? 如何使我的超级视图调整大小,以使其子视图的大小与Autolayout相匹配? - How do I make my superview resize to match the size of its subviews with Autolayout? 相对于同级UIView的iOS自动布局 - iOS Autolayout relative to the sibling UIViews programatically 如何在代码中的AutoLayout中向右移动两个UIViews 270px? - How do I shift two UIViews 270px to the right in AutoLayout in code? 如何获取UIImageView的边缘以捕捉到屏幕的边缘(超级视图)? - How can I get the edges of a UIImageView to snap to the edges of the screen (superview)? AutoLayout:在可以调整大小的超级视图中垂直居中两个UIView - AutoLayout: Vertically centering two UIViews in a superview that can resize 如何将单元格边界与超级视图的边界匹配? - How do I match cell boundary to bounds of superview? 如何设置AutoLayout约束的常量占父视图的百分比? - How can I set AutoLayout constraints' constant as a percentage of the superview? 如何使用自动布局在控制器中移动UIViews - How to move UIViews in a controller using autolayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM