简体   繁体   中英

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: 在此处输入图片说明

Constraints:

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

    在此处输入图片说明

    • View (option 1 wrapper view)

      在此处输入图片说明

      • Button (option 1 button)

        在此处输入图片说明

      • Label (option 1 label)

        在此处输入图片说明

    • View (option 2 wrapper view)

      在此处输入图片说明

      • Button (option 2 button)

        在此处输入图片说明

      • Label (option 2 label)

        在此处输入图片说明

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
    }

}

I think your buttons do not have any top and bottom constraints to their superviews' edges. Thus, their superviews (the two wrapper views) do not know their height.

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).

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM