Extra bottom space/padding on iPhone X?
Asked Answered
E

7

51

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 ?

Emia answered 15/9, 2017 at 12:46 Comment(2)
see this for ref : medium.com/@hacknicity/…Spatterdash
look at this might be helpful medium.com/the-traveled-ios-developers-guide/…Dip
W
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)

Wharfinger answered 15/9, 2017 at 13:17 Comment(0)
K
47

In Objective-C

if (@available(iOS 11.0, *)) {
   UIWindow *window = UIApplication.sharedApplication.keyWindow;
   CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
Kress answered 19/10, 2017 at 11:43 Comment(3)
YES! The view.safeAreaInsets.bottom returned 0, your tip return correctly: 34.Attainment
I echo the other user. YES! "The view.safeAreaInsets.bottom returned 0, your tip return correctly: 34." applied to me as wellSouthland
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.Southernmost
S
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.

Slide answered 20/11, 2018 at 11:6 Comment(0)
P
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
}
Patman answered 18/10, 2017 at 14:33 Comment(0)
E
6

iOS 13+ (a mix from answers above)

let padding = UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0
Emersion answered 30/4, 2019 at 20:2 Comment(0)
A
5
UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0

iOS 13 and up

Anonymous answered 11/2, 2020 at 1:33 Comment(1)
This should be higher since keyWindow is depreciated as of iOS 13Alcina
S
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.

Synopsis answered 28/7, 2018 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.