UITableView is starting with an offset in iOS 7

UitableviewIos7

Uitableview Problem Overview


I have dragged a plain jane UITableView onto a UIViewController in iOS 7.

Now there is an vertical offset of space before the first cell starts. How do I get rid of it? I want the first line to be much closer to the top edge of where the UITableView actually starts. I did not ask for the large offset did I?

enter image description here

Any ideas?

Uitableview Solutions


Solution 1 - Uitableview

The new iOS 7 implementation of UIViewController has a new set of options that allows the developer to choose if the system will automatically add insets for UIScrollView, UITableView and derivations.

To disable this behaviour uncheck these boxes for all your wanted UIViewControllers in InterfaceBuilder, on UIViewController selected object inspector:

View Controller Inspector

For more details:

  1. Submit your iOS 7 apps today.
  2. iOS 7 UI Transition Guide > Appearance and Behavior

Solution 2 - Uitableview

By default table view controllers will pad the content down under the nav bar so you could scroll the content under it and see it, in a blurred state, underneath the navbar/toolbar.

Looks like you're positioning it at 44 (maybe 64)px to move it out from under the nav bar, but it already compensates for this so you get a big gap.

Go to the storyboard/xib in IB and untick the show content under nav bar stuff.

Solution 3 - Uitableview

From iOS7 transition guide:

> If you don’t want a scroll view’s content insets to be automatically > adjusted, set automaticallyAdjustsScrollViewInsets to NO. (The default > value of automaticallyAdjustsScrollViewInsets is YES.)

   self.automaticallyAdjustsScrollViewInsets = NO;

Solution 4 - Uitableview

i had a similar problem, after dismissing a viewController, the contentOffset from my tableView was changed to (0, -64).

my solution was a little weird, i tried all the other answers but had no success, the only thing that fixed my problem was to switch the tableView position in the controls tree of the .xib

it was the first control in the parent View like this:

before

I moved the tableView right after the ImageView and it worked:

after

it seems that putting the table view in the first position was causing the trouble, and moving the table view to another position fixed the problem.

P.D. I'm not using autoLayout neither storyboards

hope this can help someone!

Solution 5 - Uitableview

it resolve my similar problem:

if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
}

Solution 6 - Uitableview

Try using this

tableView.separatorInset = UIEdgeInsetsZero;

Obviously, if you're supporting anything less than iOS7 you will need to ensure that the object responds to this selector before calling it.

Solution 7 - Uitableview

Seriously, changing contentOffset is not the solution. You're just patching a problem and not fixing the cause. I've been facing the same problem and it turns out that grouped tableViews have a padding on the top. In my case setting the type to plain has done the trick.

Hopefully it saves someone a few minutes. Z.

Solution 8 - Uitableview

With iOS 9, none of the other answers from this page worked for me (i.e. unchecking boxes in Storyboard, setting automaticallyAdjustsScrollViewInsets to NO).

My workaround I am really dissatisfied about was this:

- (void)viewDidAppear:(BOOL)animated {
    self.tableView.contentOffset = CGPointMake(0.0, 0.0);
    self.tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}

The same lines in viewWillAppear or viewDidLoad were ineffective.

Solution 9 - Uitableview

Sometimes, I get a 64 height gap at the top of my Table View when the UIViewController is embedded inside a Navigation Controller.

enter image description here

In the past, I would just re-create everything, hoping that the constraints turn out correct after a clean slate.

TIL: If you don't want to make a vertical constraint to the Top Layout Guide, you can hold down the Option key to access the Container Margin.

enter image description here

Then make sure the Top Space to Superview constant is set to 0. This worked for me at least.

enter image description here

Solution 10 - Uitableview

THis works for me:

- (void)loadView
{
	[super loadView];

	[self setAutomaticallyAdjustsScrollViewInsets:YES];

	self.edgesForExtendedLayout = UIRectEdgeNone;
	self.view.frame = CGRectZero;
	self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

Solution 11 - Uitableview

If you add an empty UIView before UITableView (or any view is scrollable such as ScrollView and TextView), you can have a luck.

Solution 12 - Uitableview

I had a UITableViewController embedded in a container view. To get rid of the unwanted 64 points of vertical space, I needed to uncheck the 'Adjust Scroll View Insets' in Interface Builder and set the UITableView's contentInset in my UITableViewController's viewWillAppear as below.

The size of the vertical space appears to match the navigation bar's frame height and y offset. The problem only occurred on iOS 7.

- (void)viewWillAppear:(BOOL)animated;
{
	[super viewWillAppear:animated];

	if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
		const CGRect navBarFrame = self.navigationController.navigationBar.frame;
		const CGFloat blankVerticalSpace = navBarFrame.origin.y + navBarFrame.size.height;
		self.tableView.contentInset = UIEdgeInsetsMake(-blankVerticalSpace, 0, 0, 0);
	}
}

Solution 13 - Uitableview

In Xamarin iOS, I had this issue occuring on a backgrounded UITableViewController just after the foreground modal dialog was being dismissed. In the process of moving into the foreground, the UITableViewController had insets set (somewhere by iOS):

This class solved it

public class UITableViewControllerWithBugFix : UITableViewController {
	public UITableViewControllerWithBugFix(UITableViewStyle withStyle) : base(withStyle) {
	}
		
	public override void ViewWillLayoutSubviews() {
		if (TableView.ContentInset.Top != 0.0f)
			TableView.ContentInset = UIEdgeInsets.Zero;
		if (TableView.ScrollIndicatorInsets.Top != 0.0f)
			TableView.ScrollIndicatorInsets = UIEdgeInsets.Zero;

		base.ViewWillLayoutSubviews();
	}
}

Solution 14 - Uitableview

From time to time I get back to this awful situation, and I notice that it's still quite unknown. So, for future memory...

Lately, I'm fixing with this workaround.

  1. Add a subview at the top of the UITableView, with zero pixel height. Works with Autolayout or without.
  2. If I feel confident :-) I remove this fake view, fix the constraints on the top, and magically it works.

Don't ask me why, I still think it's a bug deep in UIKit.

Solution 15 - Uitableview

If you go to storyboard, you can change the offset by selecting the table and under the Table View section in the Attributes inspector you just change the Separator Insets on the left to 0.

Solution 16 - Uitableview

I think the real solution is to set up your top and bottom constraints on the tableview be equal to the topMargin and bottomMargin. Not the top layout guide and bottom layout guide. This allows you to keep automaticallyAdjustsScrollViewInsets to be true.

Solution 17 - Uitableview

Swift 3 solution:

class PaddingLessTableView : UITableView
{
    override func headerView(forSection section: Int) -> UITableViewHeaderFooterView?
    {
        return nil
    }
    
    override func footerView(forSection section: Int) -> UITableViewHeaderFooterView?
    {
        return nil
    }
}

Solution 18 - Uitableview

If you embedded your TableViewController in a NavigationController or a ContainerView, You have to constraint to margins instead of top Layout guide in Storyboard. Check constraint to margins when you are doing the constraints No other way worked for me. I couldn't comment so I'm just reiterating on Robert Chens answer.

Solution 19 - Uitableview

I tried several of the answers. Changing the settings in storyboard caused ripple problems with an overlay menu that pops in from the left.

I only have a blank UIViewController in storyboard, otherwise everything is programmatically generated.

I have same problem with a UITableView inside a UIView inside a UIViewController. Namely, the section headers start too far down when the UIViewController is embedded in a Navigation Controller. W/o the navigation controller everything works fine.

To fix the problem I created a UILabel and with constraints placed the UILabel bottom constraint = the top constraint of the UIView (so it does not show on the screen. Now with that additional control (the new Label) the TableView behaves properly.

    inputsContainerView.addSubview(titleLabel)
    inputsContainerView.addSubview(tableView)
    
    // inputsContainerView
    ///////////////////////////////////////
    inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
    inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -40).isActive = true
    inputsContainerView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.7).isActive = true
    
    // tableView
    ///////////////////////////////////////
    tableView.centerXAnchor.constraint(equalTo: inputsContainerView.centerXAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: inputsContainerView.topAnchor).isActive = true
    tableView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
    tableView.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor).isActive = true
    
    // titleLabel - inserted to stop bad section header behavior
    ///////////////////////////////////////
    titleLabel.centerXAnchor.constraint(equalTo: inputsContainerView.centerXAnchor).isActive = true
    titleLabel.bottomAnchor.constraint(equalTo: inputsContainerView.topAnchor).isActive = true
    titleLabel.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
    titleLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true

Solution 20 - Uitableview

I had a same problem in iOS 11 and xib, UITableviewController and I solved it as below

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

Solution 21 - Uitableview

If none of the above answers work, try changing the table view style to plain from grouped

enter image description here

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
Questionuser798719View Question on Stackoverflow
Solution 1 - UitableviewxudreView Answer on Stackoverflow
Solution 2 - UitableviewCocoadelicaView Answer on Stackoverflow
Solution 3 - UitableviewsovanlandyView Answer on Stackoverflow
Solution 4 - UitableviewChuy47View Answer on Stackoverflow
Solution 5 - UitableviewAlexView Answer on Stackoverflow
Solution 6 - UitableviewDavid AttaieView Answer on Stackoverflow
Solution 7 - UitableviewZoltánView Answer on Stackoverflow
Solution 8 - UitableviewDirty HenryView Answer on Stackoverflow
Solution 9 - UitableviewRobert ChenView Answer on Stackoverflow
Solution 10 - UitableviewyeahdixonView Answer on Stackoverflow
Solution 11 - UitableviewCastorView Answer on Stackoverflow
Solution 12 - Uitableviewuser2067021View Answer on Stackoverflow
Solution 13 - UitableviewHerman SchoenfeldView Answer on Stackoverflow
Solution 14 - UitableviewklauslanzaView Answer on Stackoverflow
Solution 15 - UitableviewNyctitropistView Answer on Stackoverflow
Solution 16 - UitableviewDanView Answer on Stackoverflow
Solution 17 - UitableviewhhammView Answer on Stackoverflow
Solution 18 - Uitableviewuser3296487View Answer on Stackoverflow
Solution 19 - UitableviewjohnView Answer on Stackoverflow
Solution 20 - UitableviewdobihoView Answer on Stackoverflow
Solution 21 - UitableviewiOS_nerdView Answer on Stackoverflow