36
votes

I'm picking up some iOS programming and am trying to put a UITableView into a storyboard. Unfortunately, I am trying to put the content at the top of the view but it is putting in some space. I have tried to adjust the values in the inspector for View -> Mode but this doesn't seem to have any effect.

I have made the background green and put a border color to show the issue. I'm not a sophisticasted iOS dev, so I'd assume that this is the simplest solution and not something complex. How do I make the contents of the table view sit flush with the top? I've seen this Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7 but not sure if it's related.

thx for any help

enter image description here


Edit #1

Updated with changes and screen shot of properties for this table view

enter image description here

enter image description here

enter image description here

12

12 Answers

64
votes

Yes, that other question is very much related. UITableViewStyleGrouped divides each section into a "group" by inserting that extra padding…similar to what it did pre-iOS7, but clear instead of colored by default, and just at the top instead of all the way around. If you don't want the padding by default, use UITableViewStylePlain.

Otherwise, if you need to keep the style the same, do what this other posted from that link recommended and change the content inset:

self.tableView.contentInset = UIEdgeInsetsMake(-36, 0, 0, 0);

Or do what this poster suggested and set the tableHeaderView's height to .-1, i.e. nearly 0:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.01f)];
58
votes

Go to the attributes inspector of the View Controller by selecting the xib or the controller in Storyboard. Uncheck the Adjust Scroll View Insets in Layout. It will solve the problementer image description here

8
votes

Everybody keeps talking about this self.automaticallyAdjustsScrollViewInsets option.
However this did not work for me at all. What did work for me, was setting the "Content Insets" property to Never.

screenshot demonstrating where to find the setting

Such an annoying problem.

6
votes

In Swift, you just need to set the below property to false.

self.automaticallyAdjustsScrollViewInsets = false
5
votes

It is very easy and worked for me perfectly. Select a controller in Story Board in which you placed UITableView.

YouStoryboard.storyboard > YouViewController > Attributes inspector > Uncheck - Adjust scroll view insets.

Make sure you select UIViewController not UITableView.

1
votes

Swift :

override func viewWillAppear(animated: Bool) {
        self.edgesForExtendedLayout = UIRectEdge.None

       OR

self.moduleListTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

       OR

 self.automaticallyAdjustsScrollViewInsets = false
        }
1
votes

try this....

self.tableView.contentInset = UIEdgeInsetsMake( 20, 20 , 0, 0)
1
votes

in swift 3.0, i hope will help you

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}
1
votes

This is how it can be fixed easily through Storyboard:

Select Table View > Size Inspector > Content Insets: Never

0
votes

Cause of this issue:-

  1. a UITableView doesn't like to have a header with a height of 0.0. If what's you're trying to do is to have a header with a height of 0, you can jump to the solution.
  2. even if later you assign a non 0.0 height to your header, a UITableView doesn't like to be assigned a header with a height of 0.0 at first.

In ViewDidLoad:-

self.edgesForExtendedLayout = UIRectEdge.None

self.automaticallyAdjustsScrollViewInsets = false

No Need For Something Like This :-

self.myTableview.contentInset = UIEdgeInsetsMake(-56, 0, 0, 0)

In heightForHeaderInSection delegate:-

if section == 0
    {
        return 1
    }
    else
    {
        return 40; // your other headers height value
    }

In viewForHeaderInSection delegate :-

if section == 0 
{  
   // Note CGFloat.min for swift
   // For Objective-c CGFLOAT_MIN 
   let headerView = UIView.init(frame: CGRectMake(0.0, 0.0, self.myShaadiTableview.bounds.size.width, CGFloat.min)) 
   return headerView
}
else
{ 
   // Construct your other headers here 
}
0
votes

Maybe you have more than one parent navigation controller? If that's the case, uncheck other parent navigation controllers' "Show Navigation Bar".

enter image description here

0
votes

This worked for me in swift:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))