timpone
timpone

Reputation: 19979

why extra space is at top of UITableView - simple

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

Upvotes: 39

Views: 43438

Answers (13)

Amal T S
Amal T S

Reputation: 3415

For iOS 15.0+,

use this for removing extra padding at the top

if #available(iOS 15.0, *){ 
    self.tableView.sectionHeaderTopPadding = 0.0 
}

Upvotes: 0

Zvonimir Taslak
Zvonimir Taslak

Reputation: 1

This worked for me in swift:

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

Upvotes: 0

Sir Wellington
Sir Wellington

Reputation: 596

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.

Upvotes: 9

cinnamonlao
cinnamonlao

Reputation: 27

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

Upvotes: 0

Lyndsey Scott
Lyndsey Scott

Reputation: 37300

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

Upvotes: 66

Saeed Ir
Saeed Ir

Reputation: 2332

This is how it can be fixed easily through Storyboard:

Select Table View > Size Inspector > Content Insets: Never

Upvotes: 1

AzSh
AzSh

Reputation: 135

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
}

Upvotes: 1

Mohsin Qureshi
Mohsin Qureshi

Reputation: 1213

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.

Upvotes: 5

Ashwin Felix
Ashwin Felix

Reputation: 213

try this....

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

Upvotes: 1

Ashish P
Ashish P

Reputation: 1473

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 
}

Upvotes: 0

Nagendra Rao
Nagendra Rao

Reputation: 7152

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

self.automaticallyAdjustsScrollViewInsets = false

Upvotes: 6

Alvin George
Alvin George

Reputation: 14292

Swift :

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

       OR

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

       OR

 self.automaticallyAdjustsScrollViewInsets = false
        }

Upvotes: 1

Muhammad_Awaab
Muhammad_Awaab

Reputation: 1588

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

Upvotes: 58

Related Questions