Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7

前端 未结 30 2488
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 12:47

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

Here is an example:<

相关标签:
30条回答
  • 2020-11-22 13:07

    A lot of the previous answers above are too hacky. They would break at anytime in the future if Apple decides to fix this unexpected behavior.

    Root of the 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.

    Solution:

    Then, the most simple and reliable fix is to ensure that your header height is not 0 when you assign it to your table view.

    Something like this would work:

    // Replace UIView with whatever class you're using as your header below:
    UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, CGFLOAT_MIN)];
    self.tableView.tableHeaderView = tableViewHeaderView;
    

    Something like this would lead to the issue at some point (typically, after a scroll):

    // Replace UIView with whatever class you're using as your header below:
    UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = tableViewHeaderView;
    
    0 讨论(0)
  • 2020-11-22 13:09

    Storyboard:

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

    enter image description here

    Code:

    self.automaticallyAdjustsScrollViewInsets = false
    
    0 讨论(0)
  • 2020-11-22 13:10

    This is how it can be fixed easily in iOS 11 and Xcode 9.1 through Storyboard:

    Select Table View > Size Inspector > Content Insets: Never

    0 讨论(0)
  • 2020-11-22 13:12

    Uncheck "Adjust Scroll View insets"

    0 讨论(0)
  • 2020-11-22 13:12

    According to this transition guide for iOS7 by Apple, the scroll view’s content insets is automatically adjusted. The default value of automaticallyAdjustsScrollViewInsets is set to YES.

    The UIViewController which has the UITableView should set this property to NO.

    self.automaticallyAdjustsScrollViewInsets = NO;
    

    This will do the trick.

    EDIT 1:

    Also, one could try -

    self.navigationController.navigationBar.translucent = YES;
    

    This also removes the extra padding on the top.

    0 讨论(0)
  • 2020-11-22 13:12

    This code worked for me, The best answer for me that was written in objective-C at up-side so I converted it into Swift.

    For Swift 4.0+

    self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: 0.01))
    

    Just write this into viewDidLoad() and it will work like a charm.

    0 讨论(0)
提交回复
热议问题