2017-06-20 76 views
1

我正在嘗試使用行高和單元格的Autolayout組合來製作可擴展單元格。爲了讓事情變得簡單,我開始用下面簡單的代碼,我預期工作:使用Autolayout的可擴展UITableView單元格導致UIViewAlertForUnsatisiableConstraints

import UIKit 

class ViewController: UITableViewController { 

    let cells = [Cell(), Cell(), Cell()] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 50 

     for cell in cells { 
      cell.tableView = tableView 
     } 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     return cells[indexPath.row] 
    } 
} 

class Cell: UITableViewCell { 

    var cons: NSLayoutConstraint? 
    var tableView: UITableView? 

    init() { 
     super.init(style: .default, reuseIdentifier: nil) 

     let button = UIButton() 
     contentView.addSubview(button) 
     button.addTarget(self, action: #selector(Cell.tapped(_:)), for: .touchUpInside) 
     button.setTitle("Press me", for: .normal) 
     button.setTitleColor(.red, for: .normal) 

     button.translatesAutoresizingMaskIntoConstraints = false 

     // constraining the button to the view, so that buttons height would define cell height 
     let constraints = [ 
      button.topAnchor.constraint(equalTo: contentView.topAnchor), 
      button.rightAnchor.constraint(equalTo: contentView.rightAnchor), 
      button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), 
      button.leftAnchor.constraint(equalTo: contentView.leftAnchor), 
     ] 

     NSLayoutConstraint.activate(constraints) 
     cons = button.heightAnchor.constraint(equalToConstant: 100) 
     cons?.isActive = true 
    } 

    func tapped(_ sender: UIButton) { 
     // change the height of the button, thus of the contentView too (since the button is constrained to the contentView) 
     if cons?.constant == 100 { 
      cons?.constant = 200 
     } else { 
      cons?.constant = 100 
     } 
     // tell the tableView to redraw itself to apply the change 
     tableView?.beginUpdates() 
     tableView?.setNeedsDisplay() 
     tableView?.endUpdates() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

現在,這似乎不過是工作,我還是做得到UIViewAlertForUnsatisfiableConstraints以下消息警告:

(
"<NSLayoutConstraint:0x17408c7b0 V:|-(0)-[UIButton:0x109e10290'Press me'] (active, names: '|':UITableViewCellContentView:0x109e0fd90)>", 
"<NSLayoutConstraint:0x17408c8a0 UIButton:0x109e10290'Press me'.bottom == UITableViewCellContentView:0x109e0fd90.bottom (active)>", 
"<NSLayoutConstraint:0x17408c990 UIButton:0x109e10290'Press me'.height == 200 (active)>", 
"<NSLayoutConstraint:0x17408db10 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x109e0fd90.height == 100 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17408c990 UIButton:0x109e10290'Press me'.height == 200 (active)> 

似乎估計(或當前)行高與我提供的自動佈局約束相沖突。不過,令人耳目一新使用下面的代碼將顯示爲預期的tableView:

tableView?.beginUpdates() 
tableView?.setNeedsDisplay() 
tableView?.endUpdates() 

如何刪除警告?

我知道簡單的改變高度限制爲999會刪除該警告的優先級,但在我看來,作爲一個黑客刪除它,而不是作爲一種解決方案。如果我錯了,請解釋。謝謝。

+0

變化prority到的高度約束下 –

回答

1

高度約束的優先級設置爲999可以感覺像一個黑客,但根據蘋果的文檔:

注意您不一定非要使用所有1000個優先值。實際上,優先級應該圍繞系統定義的低(250),中(500),高(750)和必需(1000)優先級進行總體分組。您可能需要制定高於或低於這些值一個或兩個點的約束,以幫助防止關係。如果你遠遠超出這個範圍,你可能想重新審視你的佈局邏輯。

可能有點不是直觀的少:

「我希望按鈕的高度,以控制細胞的高度,所以其優先????」

但是,這確實出現了「正確」的方式做到這一點。

價:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html#//apple_ref/doc/uid/TP40010853-CH9-SW19

-2

刪除高度約束,即200,因爲它與頂部和底部約束相沖突。

希望它可以幫助..

+0

高度約束定義該單元的膨脹/收縮的高度。它必須在那裏。 –

相關問題