[英]Subview Constraints breaking Parent Constraints
我正在制作一個基於約束自動調整大小的自定義選項卡視圖,當我嘗試向子視圖添加也使用約束的子視圖時遇到了問題。
如果我添加一個子視圖並賦予它約束,例如:
- (void)awakeFromNib {
[super awakeFromNib];
UIBlurEffect* blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
background = [[UIVisualEffectView alloc] initWithEffect:blur];
NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:background
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1
constant:0];
NSLayoutConstraint* leadConstraint = [NSLayoutConstraint constraintWithItem:background
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0];
NSLayoutConstraint* trailConstraint = [NSLayoutConstraint constraintWithItem:background
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
NSLayoutConstraint* botConstraint = [NSLayoutConstraint constraintWithItem:background
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[background setFrame:self.bounds];
[self addSubview:background];
[self addConstraints:@[topConstraint, leadConstraint, trailConstraint, botConstraint]];
}
選項卡視圖將不再響應約束。 如果我將選項卡視圖添加到情節提要中並嘗試在其上設置約束,則它不會響應。 我可以手動調整它的大小以使其適合約束,但是它不會自動執行,更改設備當然會打破約束。
刪除添加約束的這一行可以解決此問題,但是我希望子視圖根據約束自動調整大小。
//[self addConstraints:@[topConstraint, leadConstraint, trailConstraint, botConstraint]];
這是否可能,或者在調用布局更新時是否只需要使視圖調整其所有子視圖的大小?
正如@DonMag所說,background.translatesAutoresizingMaskIntoConstraints = NO; 也許是正確的答案。
或者,您可以嘗試刪除addConstraints,並使用topConstraint.active = YES等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.