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)
CodePudding user response:
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
}
}
CodePudding user response:
let window = UIApplication.shared.windows[0]
let topPadding = window.safeAreaInsets.top
let bottomPadding = window.safeAreaInsets.bottom
print(topPadding)
print(bottomPadding)