更改tableview

时间:2015-11-26 10:43:44

标签: ios swift constraints

我使用以下代码更改按钮的约束。通常,如果我将它放在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 )>")

1 个答案:

答案 0 :(得分:0)

试试这个:

  1. 在文件检查器
  2. 中的Xcode中创建常量
  3. 从文件检查器(控制+拖动)拖动约束并创建出口
  4. 稍后您viewDidLoad只需输入your_constraint.constant = 200
  5. 即可

    就是这样。