I am just wondering whether or not it is possible to shift a UITableView
down the page by, say, maybe 50 pixels. I know this would usually work if I had used a UIViewController
then added a table view on top, but would I be able to do this and still keep it as UITableViewController
?
-
question makes no sense a bit more explanation ? what u mean by "keep it as UITableViewController" – Spring Sep 04 '11 at 17:53
-
2What I mean is, If i have a UIViewController and add a TableView, I can position the table view. However, when using a UITableViewController, I dont know how I would go about shifting the table down as when you select it in the .xib file, the x and y co-ordinates are not editable. – Michael M Sep 04 '11 at 17:56
-
Is there a reason you can't switch to UIViewController with a UITableView subview? It gives you a lot more flexibility. – James P Apr 19 '13 at 12:56
8 Answers
I had the same problem and the answer above didn't work. This did:
[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];

- 6,240
- 4
- 35
- 33
-
-
But one can still scroll on the inseted area. How do I make it unscrollable? – Ray Tso Mar 13 '17 at 08:18
-
For Swift, it looks like this: self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0) – Zion Mar 17 '17 at 11:42
-
Valid answer works for me, in Swift4.1, Xcode 9.3, it changes to `self.tableView.contentInset = UIEdgeInsets(top: 84, left: 0, bottom: 0, right: 0)` – infinity_coding7 Sep 21 '18 at 14:11
My solution is to override tableViewcontroller's method viewWillLayoutSubviews
- (void) viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.tableView.frame = CGRectMake(0,0,CGRectGetWidth(self.view.frame),300);
}
Works great and always for me with changing orientations and in all situations

- 3,133
- 1
- 29
- 31
A UITableView
is actually a UIScrollView
. This means that you can scroll the UITableView
to the point you want. This is a previous link which shows you how to do this, including sample code and discussion.
Edit: In order to shift the WHOLE tableview down, just use:
float yOffset = 50.0f; // Change this how much you want!
tableview.view.frame = CGRectMake(tableview.view.frame.origin.x, tableview.view.frame.origin.y + yOffset, tableview.view.frame.size.width, tableview.view.frame.size.height);
Hope that Helps!
-
Yeah understand that, I'm not wondering how to scroll it down, im wondering how i can shift the whole table down the view, to allow for some space for some text at the top. Thanks for the reply! – Michael M Sep 04 '11 at 18:52
-
You add it where you want it to be called. So if you want to call it in the beginning, before the user does anything, put it in the `viewDidLoad:` method. If you want it to be tied with a button, put it in the buttons `IBAction`, etc. – msgambel Sep 05 '11 at 21:25
-
If he puts it on the viewDidLoad: the entire view is likely to have a very unpleasant "jump" effect... – Jose Luis Sep 06 '11 at 11:17
-
This doesnt work for me, however it seems like a decent solution and its the best answer so far, thanks! Ive decided im just gonna work around the problem, and keep it how it is. Thanks alot though – Michael M Sep 07 '11 at 19:45
If you are trying to add a UI element at the top of the table, why not just set it to the tableHeaderView
instead?
UILabel *someLabel;
// configure label
self.tableView.tableHeaderView = someLabel;

- 7,430
- 11
- 40
- 53
Since a Table View is backed by a UIScrollView you can move in around using the content Offset.
self.tableView.contentOffset = CGPointMake( x, y);
You might want to wrap in a UIView animation

- 6,507
- 2
- 26
- 27
Swift 2.2:
To shift the tableView inside a UITableViewController down:
let edgeInsets = UIEdgeInsetsMake(20, 0, 0, 0)
self.tableView.contentInset = edgeInsets

- 833
- 9
- 6
If you need a view behind (or on top of) the tableview, then you'll have to subclass UIViewController
instead and add a UITableView
afterwards.
Another solution could be to set the table's header view (reference) but in this case, keep in mind that this view will scroll together with the table.
More information about the limitations of UITableViewController
in this article: "Clean table view code".

- 10,634
- 6
- 53
- 88
UITableViewController is actually a UIViewController, only plus is it gives you some methods to override and useful for table actions. so you can do whatever you want
check this, once you get the idea of what UITableViewController actully is you will do whatever you want
http://cocoawithlove.com/2009/03/recreating-uitableviewcontroller-to.html

- 11,333
- 29
- 116
- 185