iOS 获取安全区高度

9,057 阅读1分钟
// 获取顶部安全区高度
- (CGFloat)getSafeAreaTop {
    if (@available(iOS 11.0, *)) {
        return self.view.safeAreaInsets.top;//44
    } else {
        return 0.0;
    }
}

// 获取底部安全区高度
- (CGFloat)getSafeAreaBottom {
    if (@available(iOS 11.0, *)) {
        return self.view.safeAreaInsets.bottom;//34
    } else {
        return 0.0;
    }
}

// 获取window顶部安全区高度
- (CGFloat)getWindowSafeAreaTop {
    if (@available(iOS 11.0, *)) {
        return [UIApplication sharedApplication].delegate.window.safeAreaInsets.top;//44
    }
    return 0.0;
}

// 获取window底部安全区高度
- (CGFloat)getWindowSafeAreaBottom {
    if (@available(iOS 11.0, *)) {
        return [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;//34
    }
    return 0.0;
}