1

I have a UITableview with a custom cell, using universal/unified storyboards. For the iPad, I would like the row height to be 120, and be 70 for all other size classes.

I don't see a direct way to change the row height in storyboard based on size classes (only 1 row height for all size classes).

The main reason I want to do this is to accommodate a bigger UIImageView for the ipad class size.

For any height any width size class: UIImageView height is 60, pinned to the top and bottom at 5 each.

For regular height and regular width size class: UIImageIvew height is 100, pinned to the top and bottom at 10 each.

Since I can only do 1 row height, adding these contraints based on size class causes warnings/errors, because these constraints are incompatible with only 1 row height.

How would I make this work with the limitation of only 1 row height for differing size classes?

Sanjay Mohnani
  • 5,947
  • 30
  • 46
mrl
  • 1,467
  • 2
  • 14
  • 22

1 Answers1

1

In iOS 8:

  • When you set top and bottom (and other) constraints on the things inside a cell, you size the cell's height automatically from the inside out. The constraints cause the cell to adopt the height needed to accommodate those constraints.

  • Constraints can be conditional based on size class; that is what the size class widget at the bottom of the canvas is for. You design the general case under w:Any h:Any, and switch to any special case and change the constraints for just that case.

So, putting those two things together, you can certainly design constraints for two different sets of size classes so as to determine the height of the cell differently.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks Matt. I wanted to know if there was a storyboard way to do this (one of the reasons I wanted to start using universal storyboards), as opposed to resizing everything in code (constraints and row height) based on the detected device. – mrl May 29 '15 at 18:56
  • The problem is that you want to do this for only _one_ row. The only way to do that is `tableView:heightForRow...`. That has nothing to do with storyboards and size classes and so on. You can set up your constraints to compensate for the changed height, but the height must be stated in code, as I said. – matt May 29 '15 at 19:22
  • Sorry I must have stated something ambiguously. All the rows for the iPad will be the same (height of 120), and all the rows for the iPhone will be the same (height of 70). I want to be able to use 1 storyboard for both size classses, but the rows will be the same height within each size class. – mrl May 29 '15 at 19:26
  • Oh! I must have misunderstood completely. Okay, in that case, yes, just use size classes and conditional constraints to generate self-sizing row heights. No problem there. – matt May 29 '15 at 19:48
  • I completely rewrote my answer! It does help to understand the question. Sorry about that. – matt May 29 '15 at 19:53
  • The problem with this, is that since there is only 1 row height defined in the storyboard, I get storyboard errors when defining 2 sets of constraints based on size class. If I set the row height to 120, I can fit the constraints for Ipad, but I can't fit the constraints for the iPhone, because the total would only come out to 70. – mrl Jun 03 '15 at 18:38
  • I'm trying to accomplish this in a somewhat roundabout way at the moment. I'm using self-sizing cells, and just setting the UIImageView's intrinsic size to placeholder (so the storyboard won't show errors). Then in code, I would set the UIImageVIew's height (60 for iphone, 100 for ipad), and the cell height would adjust. – mrl Jun 03 '15 at 19:03
  • However, this method makes the most sense for dynamically sized content. The height for the UIImageView is constant for the 2 size classes, so it would be much more intuitive and straightforward to just set the height constraint definitively in storyboard based on the size class. Normally 2 sets of constraints based on size class would be fine, but the storyboard's UITableView row height doesn't accomodate for this. – mrl Jun 03 '15 at 19:07