【问题标题】:Subview Constraints breaking Parent Constraints打破父约束的子视图约束
【发布时间】:2019-08-14 17:33:37
【问题描述】:

我正在制作一个基于约束自动调整大小的自定义选项卡视图,当我尝试向其中添加也使用约束的子视图时遇到问题。

如果我添加一个子视图并对其进行约束,例如:

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

这可能吗,还是我只需要在调用布局更新时让视图调整其所有子视图的大小?

【问题讨论】:

  • background.translatesAutoresizingMaskIntoConstraints = NO; 在你之前addConstraints?

标签: ios objective-c nslayoutconstraint


【解决方案1】:

正如@DonMag 所说, background.translatesAutoresizingMaskIntoConstraints = NO;也许是一个正确的答案。

或者你可以尝试去掉addConstraints,使用topConstraint.active = YES等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多