1

I have tableView cell with different content(views, labels, imageViews) in one cell. But in something cells content can be not full. How can i use resizing cells without removing and adding always constraints? Thanks.

illustration of the problem

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
  • here is something that might help you - http://stackoverflow.com/questions/18065938/how-to-use-auto-layout-to-move-other-views-when-a-view-is-hidden – Sasha Kozachuk Nov 10 '16 at 08:18
  • i check this, here is work with constraints, but i have very much elements and it's can be very much of code to do this – Kirill Baranov Nov 10 '16 at 08:20

2 Answers2

0

Ray Wenderlich has a fantastic tutorial on dynamic sizing of table cells that can be found here: https://www.raywenderlich.com/129059/self-sizing-table-view-cells

TL;DR You need to make sure your cell's content is pinned on all four sides to the cell's content view, as well as setting as high priority vertical hugging, greater than or equal to height constraint on your label.

Jacob King
  • 6,025
  • 4
  • 27
  • 45
  • problem is that views have two states: hide, not hide. And when it isn't hide, it have height and all constraints, and cell can't be smaller – Kirill Baranov Nov 10 '16 at 08:24
  • When you say hide, I assume you are setting the `hidden` property? Try adding an outlet for the height constraint instead and setting it to 0 to hide, then the cell will resize as expected. – Jacob King Nov 10 '16 at 08:44
  • Again work with constraints. Else view has top and bottom constraints, as you say - i need else make outlets at this items and set 0 – Kirill Baranov Nov 10 '16 at 08:53
0

One of possible solutions for this problem:

  1. Add constraints for hidden state with priority 1000
  2. Add extra constraints for resized state with lower priority (ex 750)
  3. Save constraints that is ONLY for hidden state into IBOutlet collection
  4. Save constraints that is ONLY for resized state into another IBOutlet collection

Code:

@IBOutlet var hiddenConstraints: [NSLayoutConstraint] = []
@IBOutlet var visibleConstraints: [NSLayoutConstraint] = []

func hide(_ hide: Bool) {

    for hiddenConstraint in self.hiddenConstraints {
        hiddenConstraint.isActive = hide
    }

    for visibleConstraint in self.visibleConstraints {
        visibleConstraint.isActive = !hide
    }

    self.layoutIfNeeded()
}

There is faster solution:

  1. Move content that can be hidden into container view
  2. Set height constraint for container view
  3. Change from code height constraint constant to 0 if hidden or to proper height if visible

Code:

@IBOutlet var heightConstraint: NSLayoutConstraint!

func hide(_ hide: Bool) {

    self. heightConstraint.constant = hide ? 0 : 150  //Estimated height

    self.layoutIfNeeded()
}

This is not a good approach, as it will lead to constraint crashes at runtime. So I prefer to use first one.

Also you will need to update your cell from table to move other cells up or down.

Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36