Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get safe area top and bottom insets in Swift 5

Tags:

swift

I am looking for a way to get safe area inset values for the top and bottom of the screen (to position content correctly in order to make it not covered by a notch or software home button).

I looked around stackoverflow and was able to find this approach, however I am getting deprecation warning in XCode saying that keyWindow should not be used.

let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
like image 287
Ilja Avatar asked Oct 30 '25 15:10

Ilja


1 Answers

After iOS13 keyWindow concept in iOS anymore as a single app can have multiple windows. So just take the first one :

    let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    let topPadding = window?.safeAreaInsets.top
    let bottomPadding = window?.safeAreaInsets.bottom
    
like image 66
zeytin Avatar answered Nov 01 '25 12:11

zeytin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!