Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did they add more padding to UITableVIew in iOS 11?

Is it just me or they added a lot more padding to section headers and footers of grouped UITableViews in iOS 11?

I compiled my app to run with iOS 11 and noticed the extra padding. I kinda solved it by setting contentInset.top to a negative value checking if the os is iOS 11, but this is a ugly workaround.

Is there any other better way do clear the extra padding of grouped table views in order to achieve the same results across all supported iOS? It kinda stinks to have multiple checks for such a silly thing.

Screenshot for comparison:

iOS 10 vs iOS 11 UITableView comparison

As you can see, on iOS 11 there's extra spacing between sections (yeah, those are sections!).

like image 962
Phillip Avatar asked Sep 21 '17 08:09

Phillip


People also ask

Why is there extra padding at the top of my UITableView?

Short answer is that this extra padding is probably due to the table view header (not the section header), and that UITableView doesn't like to be assigned a header with a height of 0.0.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

How do I remove spaces between cells in Swift?

You can remove this by setting a property on the table view: self. tableView.


3 Answers

This is new contentInsetAdjustmentBehavior parameter of UIScrollView which you can set it as .never to prevent extra padding

if #available(iOS 11.0, *) {
    tableView.contentInsetAdjustmentBehavior = .never
}

or in storyboard under Size Inspector

enter image description here

like image 54
Rashid Avatar answered Nov 07 '22 21:11

Rashid


In iOS 11 the section footer has a view that adds to the spacing between the sections. You would need to set the view of the footer to nil explicitly in addition to adjusting the height for the footer:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

This will make your table view to look like what you had in iOS 10 and would also not have any effect on previous versions of iOS. You can do the same for the header if you don't need the headers.

like image 27
Bms270 Avatar answered Nov 07 '22 22:11

Bms270


You just need to set the table view's header and footer view.

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
like image 2
wangjx Avatar answered Nov 07 '22 23:11

wangjx