skip to Main Content

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)

2

Answers


  1. let window = UIApplication.shared.windows[0]

    let topPadding = window.safeAreaInsets.top

    let bottomPadding = window.safeAreaInsets.bottom

    print(topPadding)

    print(bottomPadding)

    Login or Signup to reply.
  2. 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
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search