简体   繁体   中英

UITableView + Add content offset at top

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?

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)

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
}

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.

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.

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

I combined this answer with this one: https://stackoverflow.com/a/9450345/1993937

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM