【问题标题】:NSLayoutConstraints crashing on ios7 but not on ios8NSLayoutConstraints 在 ios 7 上崩溃,但在 ios 8 上没有
【发布时间】:2014-10-28 08:41:57
【问题描述】:

我在 UIViewController 中设置了一些在 ios8 上运行良好的布局约束。但是一旦我在 ios7 上运行它,我就会遇到以下错误:

*** Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8803

这是我的代码:

class DatacenterIndicatorViewController: UIViewController {

let sideMargins:Float = 12.0   
var dataCenterPollingLabel:UILabel = UILabel()
var dataCenterAlarmLabel:UILabel = UILabel()

//MARK: - Life cycle
override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(dataCenterPollingLabel)
    self.view.addSubview(dataCenterAlarmLabel)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.reloadData()
}

func reloadData() {
    self.setupAlarmLabel()
    self.setupPollingLabel()
    self.generateConstraints()
}

func setupPollingLabel() {
   // some graphic setup
}

func setupAlarmLabel() {
     // some graphic setup
}

func generateConstraints() {
    self.dataCenterPollingLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.dataCenterAlarmLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

    self.view.addConstraint(NSLayoutConstraint(item: dataCenterPollingLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
    self.view.addConstraint(NSLayoutConstraint(item: dataCenterAlarmLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
    self.view.addConstraint(NSLayoutConstraint(item: dataCenterAlarmLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: dataCenterPollingLabel, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0.0))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(NSString(format:"H:|-==%f-[dataCenterPollingLabel]-==%f-[dataCenterAlarmLabel]-==%f-|", sideMargins, sideMargins, sideMargins), options: NSLayoutFormatOptions.allZeros, metrics: nil, views: ["dataCenterPollingLabel": dataCenterPollingLabel, "dataCenterAlarmLabel": dataCenterAlarmLabel]))

}
}

我的代码有什么问题?我什至可以知道在哪里查找错误,我觉得一切都很好。

【问题讨论】:

  • 控制台中是否有无法满足的约束?
  • 不,只不过是断言失败消息
  • 在添加约束时检查某些视图的大小是否为零
  • 怎么会在 ios7 中发生,而在 ios8 中却不行?
  • 您找到问题所在了吗?我在这里遇到同样的问题..

标签: ios7 swift ios8 xcode6 nslayoutconstraint


【解决方案1】:

我在 iOS 7 版本中遇到了同样的问题。在 viewDidLayoutSubviews 方法的末尾调用 self.view.layoutIfNeeded() 函数而不是 super.viewDidLayoutSubviews() 函数解决了这个问题。

代码sn-p

override func viewDidLayoutSubviews() {

    // Your statements
    self.reloadData()

    //Write the below statement at the end of the function
    self.view.layoutIfNeeded()

}

【讨论】:

  • 这为我解决了问题。我正在更新 viewDidLayoutSubviews 中的约束并在 iOS 7 但不是 iOS 8 上崩溃。
猜你喜欢
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
相关资源
最近更新 更多