Swift, Auto Resize Custom Table View Cells

无人久伴 提交于 2019-12-01 00:29:31

Check if your constraints are like this(based on your image) :

imageView : set to Top and Leading of your container, with fix height and width values.

label : you can set it to top and horizontal space of your image, with fix height and width as well.

textView : leading to image, top space to label, trailing and bottom to container.

And keep using

tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension

in your viewWillAppear()

Update for swift 4.2

Use:

UITableView.automaticDimension

Instead of:

UITableViewAutomaticDimension
Abadii176

Make sure that the content mode is set to Scale To Fill of your UITextView Make sure that there are no height constraints for the UITextView and the card UIView

Try to add the estimated height into viewDidLoad:

override func viewDidLoad() {
    tableView.estimatedRowHeight = 100
    tableView.rowHeight = UITableViewAutomaticDimension
}

Maybe the AutoHeight is not working because of the UIView above the UITextView. Try to call the sizeToFit and layoutIfNeeded methods for the UIView in the cellForRowAt:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell
    cell.vwCard.sizeToFit()
    cell.vwCard.layoutIfNeeded()
    return cell
}

You can also try the sizeToFit and layoutIfNeeded as well for the UITextView.

Hope this works........

Set bottom of your textView with bottom of that white UIView and make sure that white UIView has left,right,top and bottom constraints :)

Same type of example is explained here programmatically....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!