Why is there extra padding at the top of my UITabl

2018-12-31 17:09发布

Starting in iOS7, there is additional space at the top of my UITableView's which have a style UITableViewStyleGrouped.

Here is an example:

enter image description here

The tableview starts at the first arrow, there is 35 pixels of unexplained padding, then the green header is a UIView returned by viewForHeaderInSection (where the section is 0).

Can anyone explain where this 35 pixel amount is coming from and how I can get rid of it without switching to UITableViewStylePlain?

30条回答
人间绝色
2楼-- · 2018-12-31 17:27

Thanks to the answer by @Aurelien Porte. Here is my solution

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 
}
查看更多
骚的不知所云
3楼-- · 2018-12-31 17:28

Storyboard:

Just uncheck: Adjust Scroll View Insets in View Controller's options

enter image description here

Code:

self.automaticallyAdjustsScrollViewInsets = false
查看更多
若你有天会懂
4楼-- · 2018-12-31 17:28

This is the solution for iOS 10 using Swift 3:

You can get rid of top and bottom paddings by implementing the following methods from the UITableViewDelegate.

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

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
   return CGFloat.leastNormalMagnitude
}
查看更多
零度萤火
5楼-- · 2018-12-31 17:28
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return CGFLOAT_MIN;
}

That's all folks!

查看更多
其实,你不懂
6楼-- · 2018-12-31 17:29

So I was trying every method here, and this time none of them helped. My case was a grouped table view on iOS 9. I don't really know why and how I found out this one, but for me, setting the tableViewHeader with a UIView with at least 0.01 height worked out. CGRectZero didn't help, nothing really helped:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: 0.01))
查看更多
孤独总比滥情好
7楼-- · 2018-12-31 17:31

To be specific, to remove tableviewHeader space from top i made these changes:

YouStoryboard.storyboard > YouViewController > Select TableView > Size inspector > Content insets - Set it to never.

enter image description here

查看更多
登录 后发表回答