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

前端 未结 30 3104
隐瞒了意图╮
隐瞒了意图╮ 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;
    

提交回复
热议问题