How to Update Nslayoutconstraint in a UItableviewcell of Dynamic Height

Change dynamic Height Constraint of Element inside UITableViewCell

Hiding a subview does not remove it from the view hierarchy or layout calculations.

If you want to hide the UITextView and have the layout update correctly, there's two options I can think of.

If your targeting iOS >= 9, you should use a UIStackView. It automatically adjusts its layout when one of its subviews is hidden which is what you need.

Otherwise you would have to remove and create constraints so that bottom of the cell is constrained to the red view (in your image) instead of the UITextView.

How to set auto layout correctly for a table cell with dynamic height?

  • If you want to avoid warning, try the code below

    class MyCellView: UITableViewCell {

    private let ImageView = UIImageView()
    private let titleView = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    super.init(style: style, reuseIdentifier: reuseIdentifier)

    contentView.addSubview(ImageView)
    contentView.addSubview(titleView)

    ImageView.translatesAutoresizingMaskIntoConstraints = false
    ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
    ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    let heightConstraint = ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6)
    heightConstraint.priority = UILayoutPriority.defaultLow
    heightConstraint.isActive = true

    titleView.translatesAutoresizingMaskIntoConstraints = false
    titleView.topAnchor.constraint(equalTo: ImageView.bottomAnchor).isActive = true
    titleView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
    titleView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    titleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true

    titleView.numberOfLines = 0

    // titleView.text = " "
    }
    }
  • If you remove the code about titleView entirely and want to have an UIImageView with dynamic height in the cell, adding bottomAnchor constraint for imageView

    class MyCellView: UITableViewCell {

    private let ImageView = UIImageView()
    private let titleView = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    super.init(style: style, reuseIdentifier: reuseIdentifier)

    contentView.addSubview(ImageView)
    contentView.addSubview(titleView)

    ImageView.translatesAutoresizingMaskIntoConstraints = false
    ImageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    ImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
    ImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
    ImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    let heightConstraint = ImageView.heightAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6)
    heightConstraint.priority = UILayoutPriority.defaultLow
    heightConstraint.isActive = true
    }
    }

Adjusting NSLayoutConstraint per cell on UITableView

One problem is you're updating your constraints in layoutSubviews, but layoutSubviews runs after auto layout. You should update your constraints in updateConstraints instead.

Another problem is Xcode, through at least version 4.6.3, is buggy. Your image view is a subview of the cell's contentView, and constraints should be between the image view and the contentView. But in nibs and storyboards, Xcode incorrectly sets the constraints between the image view and the cell. (Note: this problem was fixed in Xcode 5 so you probably don't need to worry about it if you are reading this unless you are a time traveler.)

If you poke around in your view hierarchy in the debugger, you'll discover that the cells have varying heights, but their content views all have the same height. Here are the default sizes:

Section Size  Row Position  Cell Height   contentView Height

1 sole 46 43

2 first 45 43
2 second 45 43

3+ first 45 43
3+ middle 44 43
3+ last 45 43

Thus when you rely on constraints in your nib/storyboard to position your cell's subviews, you end up with problems.

The simplest solution is to just write code (in your cell's awakeFromNib) that deletes the constraints from the nib/storyboard, and creates new constraints between the contentView and the image view (and any other subviews of the contentView.) This should eliminate the need to modify the constraint constants based on the cell's position within its section.

Update constraints of other elements when UITableView height change

If you have all required constraints to your table view and other view.

Don't change the frame of TableView to change height of it.
Instead create IBOutlet of height constraint of your tablview.

e.g. say IBOutlet name is constraintTableViewHeight,
then you can the the hight easily.

constraintTableViewHeight.constant = yourNewHeightValue
//update all constraint of your view and its inner view
self.view.layoutIfNeeded();

Refer Image to create IBOutlet for your height Constraint.

Sample Image



Related Topics



Leave a reply



Submit