UITableView + adding content on top

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

How?

+82
ios iphone cocoa-touch uikit uitableview contentoffset
Feb 26 '10 at 5:09
source share
5 answers

I'm not sure I'm following you, but I think I have the same difficulty. In my case, I have to allocate some space for ADBannerView at the top of the screen, so that I add the viewDidLoad method, which I added:

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

these are the UIEdgeInsetsMake values ​​(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) 
+279
Jul 07 2018-10-10T00:
source share

You can add an β€œempty” view of the header to the table ... this will give the original table view for the offset, but as soon as you start scrolling, the offset will disappear. Not sure what you want.

If you need a constant offset and are not using the section headers yet, you can create the offset similarly above by creating custom views for the section headers, especially if you only have one section, this can give you a constant offset view.

I can post sample code if this sounds like what you are looking for.

+5
Feb 26 '10 at 7:32
source share

I combined Jigsat's answer with:

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

at

 - (void)viewDidLoad 

therefore, the first cell is not at the top.

+4
Sep 01 '11 at 7:00
source share

It looks like you want to wrap the View around your UITableView . If you have a UITableViewController in IB, then the UITableView will be automatically set to the view of UITableViewController . You change the view property to a normal UIView and add a UITableView there and assign it an 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 all the space and you won’t even be able to add other views. Therefore you need to change

 UITableViewController.view = UIView 

and add a table to this UIView

+2
Feb 26 '10 at 8:37
source share

I combined this answer with this: stack overflow

So that the table view is displayed at the top of the content insert, so that the space at the top is not cropped due to a small scroll of the table view when the view first appears. (18 is my main break)

 [self.tableView setContentInset:UIEdgeInsetsMake(18,0,0,0)]; [self.tableView setContentOffset: CGPointMake(0, -self.songListTable.contentInset.top) animated:YES]; 
0
Jul 18 '19 at 2:09 on
source share



All Articles