2015-11-26 37 views
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)>") 
+0

嘗試將其添加到'viewWillLayoutSubviews()' –

+0

'viewWillAppear'中不存在哪個項目? 'dateLabel'?或'self.view'?你什麼時候創建它? viewWillAppear和viewDidAppear之間的可用對象應該沒有區別。什麼是崩潰? – Paulw11

+0

嗨,你是否檢查dateLabel和self.view是否存在?你能分享控制檯的日誌嗎?如果有例外 - 你能顯示它的文字嗎? – NixSolutionsMobile

回答

0

試試這個:

  1. 在Xcode中創建常數,在文件檢查器
  2. 將來自文件檢查器(控制+拖動)您的約束,並在您創建了一個出口
  3. 後來viewDidLoad只是類型your_constraint.constant = 200

就是這樣。

相關問題