4

my app is built with custom top bar instead of the UINavigationBar to satisfy some requirements that cannot be done easily with the default UINavigationBar, however, after reading the answers of this question, and changing the 3 constraints of this top bar (top, leading & trailing) to be relative to the safe area instead of the superview, I couldn't get the safe area to color itself automatically to match the color of the top bar, here is what it looks like now:

enter image description here

the first answer in the link above suggests to manually color the status bar but unfortunately this is unreasonable and it uses a private API to do so. and the second answer suggests to provide a background color for the main view but it's also unreasonable because I need a different color for the main view.

how can I let the unsafe area to color itself according to the nearest view which is connected to it with auto layout constraints ?

JAHelia
  • 6,934
  • 17
  • 74
  • 134

1 Answers1

0

Here is an example of the safe area layouts on your iPhone X.

https://1.bp.blogspot.com/-nKGb8plVgd8/Wdir-wLm6tI/AAAAAAAADmo/z3G6lWBlomASUtRJ6COYYKPdPC6KdMlBACLcBGAs/s1600/safe_area.png

If you want that your "unsafe top area" gets the same color of the nearest view color, you have two logical choices.

By using two views, one on the top with a top constraint related to the superview (according to the SafeAreaLayout activation on your storyboard) and the other just beyond the latter with its top constraint linked to the top safe area. After that, link them to your viewcontroller and manage the color into your viewDidLoad method.

The other choice is probably easier by using only one view. Set its top, leading and trailing constraints to the Superview (and not the safe area layout). To avoid an interface builder error, set a fixed height to your view.

It should work properly now.

Antoine Rucquoy
  • 128
  • 1
  • 10