Extra bottom space/padding on iPhone X?

前端 未结 7 1196
春和景丽
春和景丽 2020-12-13 05:59

On the iPhone X in portrait mode, if you set a bottom constraint to safe area to 0, you will end up with an extra space at the bottom of the screen. How do you get programma

相关标签:
7条回答
  • 2020-12-13 06:22

    use this line to become your bottom value for iPhoneX

    if #available(iOS 11.0, *) {
                let bottomPadding = view.safeAreaInsets.bottom
      }
    

    and don't forget to add this in layoutSubviews() because safeAreaInsets has the correct size in layoutSubviews() otherwise you will become wrong values.

    0 讨论(0)
  • 2020-12-13 06:38

    In iOS 11, views have a safeAreaInsets property. If you get the bottom property of these insets you can get the height of the bottom padding while on iPhone X:

    if #available(iOS 11.0, *) {
        let bottomPadding = view.safeAreaInsets.bottom
        // ...
    }
    

    (likewise for the top padding with status bar)

    0 讨论(0)
  • 2020-12-13 06:40

    In Objective-C

    if (@available(iOS 11.0, *)) {
       UIWindow *window = UIApplication.sharedApplication.keyWindow;
       CGFloat bottomPadding = window.safeAreaInsets.bottom;
    }
    
    0 讨论(0)
  • 2020-12-13 06:41
    UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0
    

    iOS 13 and up

    0 讨论(0)
  • 2020-12-13 06:45
    var bottomPadding: CGFloat = 0.0
    if #available(iOS 11.0, *) {
         let window = UIApplication.shared.keyWindow
         bottomPadding = window?.safeAreaInsets.bottom ?? 0.0
    }
    

    Now you can use bottomPadding as per your needs.

    0 讨论(0)
  • 2020-12-13 06:45

    iOS 11 (a mix from answers above)

    let padding = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0

    0 讨论(0)
提交回复
热议问题