0

How to get the notch size for a Device in swift.

Is the notch size constant height for all devices ?

var notchHeightSize: CGFloat =   44 / ScreenSize.ScreenHeight
let hasNotched: Bool? = UIDevice.current.hasNotch
dynamicHeight = hasNotched == true ? dynamicHeight :  (dynamicHeight + notchHeightSize)
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
kiran
  • 4,285
  • 7
  • 53
  • 98
  • 1
    I believe this question has already been answered here: https://stackoverflow.com/questions/52402477/ios-detect-if-the-device-is-iphone-x-family-frameless – Anwuna Dec 24 '21 at 11:32

2 Answers2

1

Below is the generic extension you can use.

extension UIApplication {
    var keyWindowInConnectedScenes: UIWindow? {
        return windows.first(where: { $0.isKeyWindow })
    }
}

extension UIView {
    var safeAreaBottom: CGFloat {
        if #available(iOS 11, *) {
            if let window = UIApplication.shared.keyWindowInConnectedScenes {
                return window.safeAreaInsets.bottom
            }
        }
        return 0
    }

    var safeAreaTop: CGFloat {
        if #available(iOS 11, *) {
            if let window = UIApplication.shared.keyWindowInConnectedScenes {
                return window.safeAreaInsets.top
            }
        }
        return 0
    }
}
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
-1

let window = UIApplication.shared.windows[0]

let topPadding = window.safeAreaInsets.top

let bottomPadding = window.safeAreaInsets.bottom

print(topPadding)

print(bottomPadding)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 24 '21 at 12:22