如何以编程方式分配UIViews Autolayout边以匹配superview?

时间:2016-10-25 11:37:48

标签: ios objective-c autolayout

我有一个模态视图,我正在添加到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]];

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您可以使用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);
}];

这很容易