Question

I have Table View in my Storyboard. So let's so I know I'm going to have a total of 5 rows. Those 5 rows to not take up the entire screen space vertically, so I would like the group of those 5 rows to be centered vertically on the screen, instead of starting from the top and having all the blank space on the button because there are only 5 rows.

How do I accomplish this (1) Using Storyboard or (2) using code?

Était-ce utile?

La solution

you could use UIScrollViews contentOffset-property, since UITableView inherits from UIScrollView. Here is a link to the docs:
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/contentOffset

Essentially you just set a Point to define the X- and Y-Offset. So if you want the TableViews content to be offset by 100pts from the top, you would set

myTableView.contentOffset = CGPointMake(0, -100); //(edited in response to Lavvo's comment)

and thats it!

EDIT: Since its a property, there is a chance that you can set it in Interface Builder, but i cant check that right now. If so, its probably in the "Sizes"-Panel, i would guess.

Autres conseils

If your table view has a static height, the easiest thing to do in a storyboard is to set the height constraint of the table view to however tall you want it. For example, if your cells are 44, 5*44 = 220.

Then add a constraint to your table view to have it centered vertically in its container.

This will make it compatable with any screen size as auto layout will automatically center it in the view container no matter what size it changes to.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top