UITableView + Add content offset at top
Asked Answered
B

6

99

I need to add some blank space to the top of my UITableView that does not affect the size of the content area. Shifting the content down or adding a blank cell is NOT what I want to do. Instead I just want an offset.

How?

Byng answered 26/2, 2010 at 5:9 Comment(0)
E
300

I'm not sure if I'm following you but I think I'm having the same predicament. In my case I must give some space to the ADBannerView at the top of the screen so what I did was in the viewDidLoad method I added:

[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];

the values it takes are UIEdgeInsetsMake(top,left,bottom,right).

Alternatively the same with Swift:

self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)

Swift 4.2:

self.tableView.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
Euphonic answered 7/7, 2010 at 3:4 Comment(3)
Needed this to not show a "loading" cell at the top, toggle back to ego inset when that cell is rendered, back to -50 top when reloadData occurs. thanks !Penult
You might want to adjust the scroll indicator as well: [self.tableView setScrollIndicatorInsets:UIEdgeInsetsMake(50,0,0,0)];Ause
Yep, or even better, adjust the scroll indicator like this: [self.tableView setScrollIndicatorInsets:[self.tableView contentInset]];Muslim
A
19

Swift 5.1

add the following in viewDidLoad

tableView.contentInset.top = 100

Really that's all there is to it.

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.contentInset.top = 100
}
Alyssa answered 12/10, 2019 at 19:19 Comment(0)
G
6

You can add an "empty" header view to the table... this would give the initial appearance of the table to have an offset, but once you started scrolling the offset would be gone. NOt sure that's what you want.

If you need a permanent offset and are not already using section headers, then you could create the offset similarly to above by making custom views for the section headers, especially if you just have one section, this could give you the look of a permanent offset.

I can post sample code if it sounds like either of those are what you are looking for.

Gavage answered 26/2, 2010 at 7:32 Comment(0)
L
4

I combined Jigzat's answer with:

[self.tableView scrollRectToVisible:CGRectMake(0, 0, 320, 1) animated:NO];

in

- (void)viewDidLoad

so the first cell isn't at the top.

Liaoyang answered 1/9, 2011 at 7:0 Comment(0)
C
2

Sounds like you want to wrap a 'View' around your UITableView. If you have a UITableViewController in IB the UITableView will automatically be set to the view of UITableViewController. You change view property to a normal UIView and add your UITableView in there and give it a offset.

---Edit--- I just read my post and thought it made little sense :) When you create a UITableViewController you get this (in pseudo code):

UITableViewController.view = UITableView

This means that the actual table will take up the whole space and you cannot even add other views. So you need to change the

UITableViewController.view = UIView

and add your table to that UIView

Coplanar answered 26/2, 2010 at 8:37 Comment(0)
H
2

I combined this answer with this one: https://mcmap.net/q/134671/-get-uiscrollview-to-scroll-to-the-top

To make the tableView appear at the top of the content inset, so the space at the top isn't cut off by having the tableView scrolled down slightly when the view initially appears. (18 is my top gap)

[self.tableView setContentInset:UIEdgeInsetsMake(18,0,0,0)];
[self.tableView setContentOffset:
 CGPointMake(0, -self.songListTable.contentInset.top) animated:YES];
Hedvig answered 18/7, 2019 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.