Jace Jace - 3 months ago 31
iOS Question

How to use Auto Layout constraints to fit 2 labels with variable size in a table cell which height is estimated using estimatedHeightForRowAtIndexPath

I have the following layout with the following constraints.

When I run it, it looks just as desired. The cell height adjusts to fit all of the text.

However, I see the following warning in the Issue Navigator and I do not understand why.

2 views are vertically ambiguous


  • Height and vertical position are ambiguous for View

  • Height is ambiguous for View



Layout:
enter image description here

Constraints:


  • View (cell wrapper view for all elements in the cell)

    enter image description here


    • View (option 1 wrapper view)

      enter image description here


      • Button (option 1 button)

        enter image description here

      • Label (option 1 label)

        enter image description here


    • View (option 2 wrapper view)

      enter image description here


      • Button (option 2 button)

        enter image description here

      • Label (option 2 label)

        enter image description here





Table View Controller code:

import UIKit

class SelectionTableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

//Hide navigation bar
self.navigationController?.navigationBarHidden = true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: Hide status bar
override func prefersStatusBarHidden() -> Bool {
return true
}

// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

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

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

}

Answer

The storyboard seems to be complaining because it doesn't know how tall the cell's view is going to be, it doesn't know you are going to supply a height at runtime using estimatedHeightForRowAtIndexPath. All your constraints are relative to the superviews or other views and as a result there are infinite ways to satisfy those.

The easiest way to just suppress those warnings is to give the content view of the cell an explicit height, and then on the right navigator check the 'remove at build time' box for that constraint (find it on the left navigator, and click on it there).

enter image description here

Giving an explicit number will allow the compiler to give explicit values to the height and y positions of those views which should resolve the warnings you are getting. Then removing that constraint should allow the cell to dynamically resize in the code using the methods you have implemented.

Comments