Search code examples
iosswiftxcode9iphone-x

How can I set background colour for top section in iPhoneX


I just realise that my app is not working for iphoneX's design. I have a UIVIew (containing textfield and button) tends to be like navigation bar so it does support for iPhone 8 and 8 plus. However, the iPhoneX has blank white for the top section. Vice versa, if I set the Uiview to act like navigation bar, then the iphone8 and 8plus will have higher gap between the textfield and top layout. May I know how can I set the constraint that fits for all version or other way to add for the top section and it also changed when landscape mode. Many thanks!

screenshot


Solution

  • If you are using Interface Builder you may want to update your constraints and their settings.

    if #available(iOS 11.0, *) {
         if UIApplication.shared.keyWindow!.safeAreaInsets.top > CGFloat(0.0) {
             topConstraint.constant += 24
         }
     }
    

    or

    enter image description here