我使用以下代码更改按钮的约束。通常,如果我将它放在viewDidAppear函数上,但是用户首先看到无约束按钮,然后按钮移动到正确的位置。
如果我将代码放到viewDidLoad或viewWillAppear,那么当这两个函数运行时,应用程序崩溃,因为没有表。
如何在向用户显示视图之前更改约束?
//in class definition, outlet for the constraint
@IBOutlet weak var dateLabelLeft: NSLayoutConstraint!
//code in viewDidLoad, viewDidAppear, viewWillAppear function
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
dateLabelLeft = NSLayoutConstraint(item: dateLabel, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 200)
} else {
dateLabelLeft = NSLayoutConstraint(item: dateLabel, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 100)
}
异常详情:
打破warnBlockingOperationOnMainThread()进行调试。 2015-11-26 13:54:10.865 testApp [56180:2698380]无法同时满足约束条件。
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. (
"<NSLayoutConstraint:0x7fc9d85820a0 UITableView:0x7fc9d88e7400.leading == UIView:0x7fc9d8559740.leadingMargin - 20>",
"<NSLayoutConstraint:0x7fc9d8585060 H:|-(0)-[UITableView:0x7fc9d88e7400](LTR) (Names: '|':UIView:0x7fc9d8559740 )>")
答案 0 :(得分:0)
试试这个:
viewDidLoad
只需输入your_constraint.constant = 200
就是这样。