0
我正在使用以下代碼更改按鈕的約束。通常,如果我將它放在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)>")
嘗試將其添加到'viewWillLayoutSubviews()' –
'viewWillAppear'中不存在哪個項目? 'dateLabel'?或'self.view'?你什麼時候創建它? viewWillAppear和viewDidAppear之間的可用對象應該沒有區別。什麼是崩潰? – Paulw11
嗨,你是否檢查dateLabel和self.view是否存在?你能分享控制檯的日誌嗎?如果有例外 - 你能顯示它的文字嗎? – NixSolutionsMobile