2

I'm trying to have a tableview resize depending on the screen size. The app is designed for iPhone 6, so it should basically shrink if the screen is smaller. I tried using constraints and pinning to superview, but it only seems to work for the height. I'm thinking I might have to resize the cells as well, but I'm not sure how to do it.

I know the question is very general, but I absolutely don't know the first thing about this.

Thanks for your help.

2 Answers2

0

You need to use autolayout for both your tableview and your cells, otherwise ios will just try to guess the missing constraints (poorly). Make sure you set both horizontal and vertical constraints.

tskulbru
  • 2,336
  • 1
  • 20
  • 38
  • That's what I thought, but I'm not sure how to do it for cells since I can't pin to superview through the editor, should I pin the cell to the tableview in code? The cell has its own xib, it's a custom cell. – Eric Hartand Dec 30 '15 at 13:36
  • As long as you have mapped all the constraints in the cell xib, and also done the same for the containing tableview etc, i dont think you need to. I usually use cell prototype in my storyboard when i set up my tables, and as such my view hierarchy is correct in regards to superviews etc. If it for some reason doesn't work for you, you might have to do it in code as you suggested. – tskulbru Dec 30 '15 at 17:24
  • Ok but how do I set the contraint for the cell xib? I can't do it in editor since there is no superview to speak of. How can I constrain it to adapt to the size of the table view without using code? – Eric Hartand Dec 31 '15 at 16:54
0

If you can disable autolayout and use AutoResizing, it might be easy. After disabling the autolayout select your table in xib and go to Size inspector on Right Pane. In that set the auto resizing as follows

Image

It can adjust the height and width automatically. No matter the size of screen is.

Additional: If you have excess of empty rows at the bottom hiding the size, you can remove it by the following code:

self.table.tableFooterView = UIView()
AAA
  • 1,957
  • 4
  • 23
  • 42