50

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 programmatically the height of this extra padding ?

I managed to manually determine the height of this padding which is 34 and here is how I managed to implement it with iPhone X detection:

Swift 4.0 and Xcode 9.0

if UIDevice().userInterfaceIdiom == .phone
{
    switch UIScreen.main.nativeBounds.height
    {
        case 2436: //iPhone X
        self.keyboardInAppOffset.constant = -34.0
        default:
        self.keyboardInAppOffset.constant = 0
    }
}

Is there a cleaner way to detect the height of this padding ?

standousset
  • 1,092
  • 1
  • 10
  • 25
  • 1
    see this for ref : https://medium.com/@hacknicity/how-ios-apps-adapt-to-the-iphone-x-screen-size-a00bd109bbb9 – Anbu.Karthik Sep 15 '17 at 13:03
  • 1
    look at this might be helpful https://medium.com/the-traveled-ios-developers-guide/iphone-x-dealing-with-home-indicator-2e8e47f5647f – Gagan_iOS Sep 15 '17 at 13:24

7 Answers7

72

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)

Paolo
  • 3,825
  • 4
  • 25
  • 41
47

In Objective-C

if (@available(iOS 11.0, *)) {
   UIWindow *window = UIApplication.sharedApplication.keyWindow;
   CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
user6788419
  • 7,196
  • 1
  • 16
  • 28
  • 3
    YES! The `view.safeAreaInsets.bottom` returned `0`, your tip return correctly: `34`. – Thomás Pereira Jan 17 '19 at 20:46
  • I echo the other user. YES! "The view.safeAreaInsets.bottom returned 0, your tip return correctly: 34." applied to me as well – Joshua Wolff Jun 05 '20 at 01:59
  • This is useful when you want to get the "real" bottom safearea. If there is a tabbar down there, then I found that the vc.view.safeAreaInsets.bottom included the tabbar height, which makes sense. But I wanted my custom action sheet to cover the tool bar but not be all the way down to the bottom. So this gets me the number I need. – Andy Weinstein Sep 15 '20 at 17:12
11
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.

Hemang
  • 26,840
  • 19
  • 119
  • 186
6

If suddenly you have no view, for example, CollectionViewLayout, you can retrieve it from Window too:

private static var itemHeight: CGFloat {
    guard #available(iOS 11.0, *),
        let window = UIApplication.shared.keyWindow else {
            return Constants.itemHeight
    }
    return Constants.itemHeight - window.safeAreaInsets.bottom
}
Azon
  • 57
  • 4
6

iOS 13+ (a mix from answers above)

let padding = UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0
Merricat
  • 2,583
  • 1
  • 19
  • 27
5
UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0

iOS 13 and up

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
user10447655
  • 180
  • 2
  • 10
2

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.

Osman
  • 1,496
  • 18
  • 22