Don't use SafeArea inset on iPhone X for first UITableViewCell
Asked Answered
A

4

9

I have a UITableView and the first row is used as a sort of header cell with a full bleed background image and other elements. For this cell I do NOT want to use the SafeArea and I want the UIView to expand all the way to the edge of the screen. Currently I get this:

enter image description here

I have tried to set this manually for the cell:

if (@available(iOS 11.0, *)) {

    headerCell.insetsLayoutMarginsFromSafeArea = NO;
    headerCell.layoutMargins = UIEdgeInsetsMake(10, 0, 10, 0);
    UIEdgeInsets i = headerCell.layoutMargins;
    NSLog(@"Left: %f", i.left);
}

Sadly this doesn't work.

Here is a mockup of what it should look like, with the first cell contents NOT being affected by the safe area:

enter image description here

Is there any way to do what I am wanting just for the first cell?

Aileen answered 4/1, 2018 at 18:15 Comment(2)
Can you not use a UITableViewHeaderFooterView rather than a UITableViewCell? Apple talks about this issue with header views in this video starting at 8 minutes in: developer.apple.com/videos/play/fall2017/201Toft
I think you are going to have to somewhat fake it by making the UITableView stretch the full width (i.e. ignoring the safe area) and then to inset the UITableViewCells back for everything except the first one.Colangelo
A
14

I found that I needed to uncheck the Content View Insets To Safe Area checkbox that is on the UITableView.

enter image description here

Doing this fixed the issue!

Aileen answered 5/1, 2018 at 0:0 Comment(1)
My understanding is that this changes the contentInsets for all the cells in the tableView. How did you manage to change the insets only on the first tableViewCell ?Perisarc
E
11

Marking the "Content insets" under Size Inspector to "Never" worked for me. enter image description here

Exultation answered 11/4, 2019 at 7:10 Comment(0)
S
5

In code, it could be accomplished by

        tableView.insetsContentViewsToSafeArea = false

Sofar answered 6/12, 2019 at 13:59 Comment(1)
You save my day dude! Thanks!Stockwell
L
4

Just go to the tableview Size inspector and look for the Content Insets by default it will be set to Automatic change that to Never as shown like in the image below enter image description here

Longevity answered 13/2, 2020 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.