简体   繁体   中英

AutoLayout Constraints For Dynamic Cell Height

I have been searching in stack overflow from long time,i found some questions related but not perfect. Can someone explain In detail what are the steps to make label grow its height based on text that is in tableview cell.I want to increase the labels height and then the cells height. Thank You.

For iOS 8+ you can find explanation and example here For iOS prior 8 version it's more complicated to do this. You should manually calculate the height of the cell and then provide this heigh in delegate methods of a table view (heighForRow at index path). Hope this will help you.

it boils down to this

 var myRowHeight: CGFloat = 100    //initialize this to some value close to what it will probably be

override func viewDidLoad() {
        //control row height for variable text size
        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = myRowHeight
    }

    override func viewWillAppear(animated: Bool) {
        //set row height right before view appears so autolayout has time to estimate it
        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = UITableViewAutomaticDimension
    }

This is the solution that works for me.

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