【问题标题】:NSLayoutConstraints breaking but seem fineNSLayoutConstraints 打破但看起来不错
【发布时间】:2016-09-28 17:13:26
【问题描述】:

我正在尝试将图像视图固定到超级视图的顶部、左侧和右侧,并使用 NSLayoutConstraint 给它一个高度,如下所示:

backgroundView.addSubview(imageView)
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: backgroundView, attribute: .Top, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Left, relatedBy: .Equal, toItem: backgroundView, attribute: .Left, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Right, relatedBy: .Equal, toItem: backgroundView, attribute: .Right, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: view.bounds.width))

但是图像没有显示,并且调试器正在记录我的约束已被破坏。我看不出是什么限制导致了这个问题。

【问题讨论】:

    标签: ios swift autolayout nslayoutconstraint


    【解决方案1】:

    通过在将 imageView 作为子视图添加到 backgroundView 之前添加 imageView.translatesAutoresizingMaskIntoConstraints = false 来修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多