4

I have a UITableViewController with 3 static cells on it in one section. I'd like to align the whole table to the bottom, not to the top. I tried to set several settings in Xcode bit could not solve it. How can I do this?

UPDATE: so, this is the alignment I want, the cells are in red: enter image description here

Thanks

Tom
  • 3,899
  • 22
  • 78
  • 137
  • align the whole table to the bottom of.... the screen? those buttons? some other view? – Michael Dautermann Jan 18 '12 at 22:12
  • Align the whole table to the bottom of the screen, and on the whole screen there is a table view controller. The cells should not be on the top, but on the bottom. – Tom Jan 18 '12 at 22:18

3 Answers3

6

If you manually compute the height of your rows, you can set the table view's contentInset (inherited from UIScrollView) to push the rows down. You'll have to manually update the inset if you change the table rows. For example, if all of your rows are the same height:

CGFloat totalRowHeight = [self.tableView numberOfRowsInSection:0] * self.tableView.rowHeight;
CGFloat topInset = MAX(0, self.tableView.bounds.size.height - totalRowHeight);
self.tableView.contentInset = UIEdgeInsetsMake(topInset, 0, 0, 0);
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • contentSize might simplify the calculation, maybe `self.tableView.bounds.size.height - self.tableView.contentSize.height`. – Peter DeWeese Aug 20 '14 at 21:04
1

You can position a table using a CGRect and setting it to the UITableView's frame property.

Here's a rough example:

CGRect someFrame = CGRectMake(<x coordinate>, <y coordinate>, <width>, <height>);
myTableView.frame = someFrame;

I'm not clear on what buttom and top means to your application. Do you mean at the bottom of its container? Bottom of the entire screen?

If you want it at the bottom of its container, then you could do this to calculate a new y position:

CGFloat yPos = myTable.superview.frame.size.height-myTable.frame.size.height;

EDIT

As @robmayoff suggests, the table is sized to fit the cells. Simply make the height of your tableview to be 44.0*3 (default height of cell x 3 cells) and modify its y position using the above example.

Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • E.g. if you see the built in map application on IPad, you can see a 3 cells selector, where you can select map mode (normal, satellite, hybrid) – Tom Jan 18 '12 at 22:20
  • It looks to me like the table view in the Maps application has been sized to be just tall enough to show all of its rows, and positioned against the bottom edge of the screen. I doubt the table view is as tall as the screen. – rob mayoff Jan 18 '12 at 22:39
1

Use a large positive value for the table view's top content inset. This will push the cells down as far as you specify.

Mark Adams
  • 30,776
  • 11
  • 77
  • 77