【问题标题】:Error when creating constraints programmatically以编程方式创建约束时出错
【发布时间】:2017-04-04 14:14:21
【问题描述】:

我有 2 个视图:aView 和 topView。这两者都具有界面构建器中的所有必要约束。

现在我需要向视图添加一个 WKWebView 实例。所以我创建了一个 webPage 属性。然后我尝试初始化这个东西并像这样添加4个约束:

self.webPage = [[WKWebView alloc] init];

NSLayoutConstraint *topConstraint =[NSLayoutConstraint
                                   constraintWithItem: self.webPage
                                   attribute:NSLayoutAttributeTop
                                   relatedBy:NSLayoutRelationEqual
                                   toItem: self.topBar
                                   attribute:NSLayoutAttributeBottom
                                   multiplier:1.0
                                   constant:0.0];


NSLayoutConstraint *bottomConstraint =[NSLayoutConstraint
                                    constraintWithItem: self.webPage
                                    attribute:NSLayoutAttributeBottom
                                    relatedBy:NSLayoutRelationEqual
                                    toItem: self.aView
                                    attribute:NSLayoutAttributeBottom
                                    multiplier:1.0
                                    constant:50.0];


NSLayoutConstraint *leadingConstraint =[NSLayoutConstraint
                                    constraintWithItem: self.webPage
                                    attribute:NSLayoutAttributeLeading
                                    relatedBy:NSLayoutRelationEqual
                                    toItem: self.aView
                                    attribute:NSLayoutAttributeLeading
                                    multiplier:1.0
                                    constant:0.0];


NSLayoutConstraint *trailingConstraint =[NSLayoutConstraint
                                    constraintWithItem: self.webPage
                                    attribute:NSLayoutAttributeTrailing
                                    relatedBy:NSLayoutRelationEqual
                                    toItem: self.aView
                                    attribute:NSLayoutAttributeTrailing
                                    multiplier:1.0
                                    constant:0.0];

[self.aView addSubview: self.webPage];

[self.aView addConstraints: @[topConstraint, bottomConstraint, leadingConstraint, trailingConstraint]];

所以我的 WKWebView 实例总是在 topView 的正下方,在 aView.bottom 上方 50 pt 并且卡在视图的边缘。

但我得到错误:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 

我不明白我做错了什么。

【问题讨论】:

    标签: ios xcode autolayout nslayoutconstraint


    【解决方案1】:

    您需要将 translatesAutoresizingMaskIntoConstraints 设置为 false

    self.webPage.translatesAutoresizingMaskIntoConstraints = false
    

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 2019-06-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多