UITableView + Add content offset at top

IosUitableviewCocoa TouchUikitContentoffset

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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
}

Solution 3 - Ios

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.

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbluefloyd8View Question on Stackoverflow
Solution 1 - IosjigzatView Answer on Stackoverflow
Solution 2 - IosRom4inView Answer on Stackoverflow
Solution 3 - Iosscottbates22View Answer on Stackoverflow
Solution 4 - IosrichyView Answer on Stackoverflow
Solution 5 - IoswillcodejavaforfoodView Answer on Stackoverflow
Solution 6 - IosTheJeffView Answer on Stackoverflow