Get safe area inset top and bottom heights

后端 未结 15 1430
长发绾君心
长发绾君心 2020-11-30 17:30

On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?

15条回答
  •  执念已碎
    2020-11-30 17:39

    Try this :

    In Objective C

    if (@available(iOS 11.0, *)) {
        UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
        CGFloat topPadding = window.safeAreaInsets.top;
        CGFloat bottomPadding = window.safeAreaInsets.bottom;
    }
    

    In Swift - iOS 13.0 and above

    // Use the first element from windows array as KeyWindow deprecated

    if #available(iOS 11.0, *) {
        let window = UIApplication.shared.windows[0]
        let topPadding = window.safeAreaInsets.top
        let bottomPadding = window.safeAreaInsets.bottom
    }
    

    In Swift

    if #available(iOS 11.0, *) {
        let window = UIApplication.shared.keyWindow
        let topPadding = window?.safeAreaInsets.top
        let bottomPadding = window?.safeAreaInsets.bottom
    }
    

提交回复
热议问题