NSLayoutConstraints在ios7上崩溃但在ios8上没有崩溃

时间:2021-03-15 23:00:43

I've setup in a UIViewController some layout constraints which works fine on ios8. But as soon as I run it on ios7 i've got the following error :

我在UIViewController中设置了一些布局约束,在ios8上工作正常。但是只要我在ios7上运行它就会出现以下错误:

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

Here is my code :

这是我的代码:

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]))

}
}

What is wrong in my code ? I can even know where to look for some errors, everything looks fine to me.

我的代码有什么问题?我甚至可以知道在哪里寻找一些错误,一切看起来都很好。

1 个解决方案

#1


12  

I faced the same issue in iOS 7 version. Calling self.view.layoutIfNeeded() function instead of super.viewDidLayoutSubviews() function at the end of the viewDidLayoutSubviews method solved the issue.

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

Code snippet

代码段

override func viewDidLayoutSubviews() {

    // Your statements
    self.reloadData()

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

}

#1


12  

I faced the same issue in iOS 7 version. Calling self.view.layoutIfNeeded() function instead of super.viewDidLayoutSubviews() function at the end of the viewDidLayoutSubviews method solved the issue.

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

Code snippet

代码段

override func viewDidLayoutSubviews() {

    // Your statements
    self.reloadData()

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

}