美文网首页iOS
iOS iPhoneX 适配

iOS iPhoneX 适配

作者: 路飞_Luck | 来源:发表于2018-10-03 18:19 被阅读573次
    一 iPhoneX各个视图值
    1.简单定义一下IPoneX
    _isIphoneX = [UIScreen mainScreen].bounds.size.height == 812 ? YES : NO;
    
    2.了解IPhoneX各个视图的高度
    • 状态栏 - statusBarHeight
    • 导航栏 - navBarHeight
    • tabBar
    • 安全距离 - safeAreaBottomHeight
    image.png

    一般比较常用的说法导航栏高度(状态栏+导航栏),tabBar高度(tabBar + 安全距离)

    1.导航栏高度 88,非iPoneX手机为64
    2.状态栏高度44,非iPoneX手机为20
    3.tabar高度83,非iPhoneX手机为49

    代码如下

    • AppEnvs.h
    @interface AppEnvs : NSObject
    #pragma mark - 相关变量值
    @property(nonatomic, assign) int screenWidth;   // 屏幕宽度
    @property(nonatomic, assign) int screenHeight;  // 屏幕高度
    @property(nonatomic, assign) int statusBarHeight; // 系统状态栏高度
    @property(nonatomic, assign) int tabBarHeight; // 系统TabBar高度 + 安全距离
    @property(nonatomic, assign) int safeAreaBottomHeight; //iPhone X 安全距离
    @property(nonatomic, assign) int navBarHeight;  // 导航栏高度
    @property(nonatomic, assign) int navHeight; // 状态栏 + 导航栏
    @property(nonatomic, assign) int screenHeightTabBar; // 屏幕高度 - 状态栏 - 导航栏 - tabBar - 安全距离
    @property(nonatomic, assign) int screenHeightTabBarNoNavBar;    // 屏高 - 状态栏高度 - tabBar - 安全距离
    @property(nonatomic, assign) int screenHeightNoNavBar;  // 屏高 - 状态栏 - 导航栏 - 安全距离
    @property(nonatomic, assign) int screenHeightNoStatusBar;   // 屏幕高度 - 状态栏
    
    #pragma mark - 机型
    @property(nonatomic, assign) bool isIPhone4;
    @property(nonatomic, assign) bool isIPhone5;
    @property(nonatomic, assign) bool isIPhone6;
    @property(nonatomic, assign) bool isIPhone6Plus;
    @property(nonatomic, assign) bool isIPhoneBig;
    @property(nonatomic, assign) bool isIphoneX;
    
    @end
    
    • AppEnvs.m
    @implementation AppEnvs
    - (instancetype)init {
        self = [super init];
        if (self) {
            _screenWidth = [UIScreen mainScreen].bounds.size.width;
            _screenHeight = [UIScreen mainScreen].bounds.size.height;
            _statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
            _safeAreaBottomHeight = _screenHeight == 812.0 ? 34 : 0;
            _navBarHeight = 44;
            
            _navHeight = _navBarHeight + _statusBarHeight;
            _tabBarHeight = ([[UIApplication sharedApplication] statusBarFrame].size.height > 20 ? 83 : 49);
            _screenHeightNoNavBar = _screenHeight - _navHeight - _safeAreaBottomHeight;
            _screenHeightTabBar = _screenHeight - _navHeight - _tabBarHeight;
            _screenHeightTabBarNoNavBar = _screenHeight - _statusBarHeight - _tabBarHeight;
            _screenHeightNoStatusBar = _screenHeight - _statusBarHeight;
            
            // 机型
            if (_screenHeight == 812) {
                _isIphoneX = YES;
            } else if (_screenWidth > 370 && _screenWidth < 400 && _screenHeight != 812) {
                _isIPhone6 = YES;
            } else if (_screenWidth > 400 && _screenHeight != 812) {
                _isIPhone6Plus = YES;
            } else if (_screenWidth == 320 && _screenHeight == 568) {
                _isIPhone5 = YES;
            } else if (_screenWidth == 320 && _screenHeight == 480) {
                _isIPhone4 = YES;
            }
            
            if (_screenWidth > 370) {
                _isIPhoneBig = YES;
            }
        }
        return self;
    }
    @end
    

    运行结果


    image.png

    附带iPhoneX和iPhone6的变量截图

    • iPhoneX各个变量说明
    iPhoneX.png
    • iPhone6各个变量说明
    iPhone6.png
    • 如有错误,欢迎指正,多多点赞,打赏更佳,您的支持是我写作的动力。

    项目连接地址 - iPhoneX-adaption


    相关文章

      网友评论

        本文标题:iOS iPhoneX 适配

        本文链接:https://www.haomeiwen.com/subject/iettaftx.html