簡體   English   中英

如何以編程方式分配UIViews自動布局邊緣以匹配超級視圖?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM