Nic Hubbard
Nic Hubbard

Reputation: 42165

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

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?

Upvotes: 9

Views: 8698

Answers (4)

Super Xtreem
Super Xtreem

Reputation: 187

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

Upvotes: 4

Jurasic
Jurasic

Reputation: 1956

In code, it could be accomplished by

        tableView.insetsContentViewsToSafeArea = false

Upvotes: 5

Vanita L.
Vanita L.

Reputation: 1325

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

Upvotes: 11

Nic Hubbard
Nic Hubbard

Reputation: 42165

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!

Upvotes: 14

Related Questions