Masonry关于Safe Area的适配.

这里引用一张苹果的图来说明什么是Safe Area安全区域.
这里写图片描述
当有导航栏和tabbar的时候.咱们的布局如果被这两个中任意一个控件覆盖.用户就会变得比较困扰.毕竟挡住了交互事件.虽说我们能在技术上使用hittest.但是毕竟反人类.所以苹果提出了一个安全区域.tabbar上.nav下(这两个控件的大小是固定的(我指的是高度,不去手动调整的话),当没有nav和tab的话手动加上相应高度就是安全区域).
在iPhone变屏幕大小之前.写页面可以直接使用frame写死就OK.但是自从苹果变得bigger than bigger之后.frame就没那么好用了.这时候可能有些人用的还是frame然后计算比例.有些人可能用约束去了.当iPhoneX出来的时候.我接手的工程我看到了if iPhoneX的判断.然后里头frame.SDAutoLayout.Masonry等等齐上阵.
对于Masonry.工程里头用的十分的不专业.是Masonry和if iPhoneX的结合来make.top.offset(xxx).或者是make.bottom.offset(-xxx);

    ///举个例子
    UIView *viewOne = [UIView new];
    [self.viewOne addSubview:backGroundView];
    if (iPhoneX) {
        [backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(8);
            make.left.right.offset(0);
        }];
    } else {
            [backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(8 + iPhoneXTopHeight);
            make.left.right.offset(0);
        }];
    }

其实不习惯Masonry也可以使用


    [backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (iPhoneX) {
            make.top.offset(8 + iPhoneXTopHeight);
        } else {
            make.top.offset(8);
        }
        make.left.right.offset(0);
    }];

这样看起来是不是简洁多了.其实对于安全区域布局.还有更方便的方法.而不用去进行计算

    ///对于工程兼容是iOS8开始的(因为safe area是iOS9开始支持的)
    ///安全区顶部
    make.top.equalTo(self.mas_topLayoutGuide).offset(8);
    ///安全区底部
    make.top.equalTo(self.mas_bottomLayoutGuide).offset(-8);
    ///对于工程支持是iOS11使用
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeading NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTrailing NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideWidth NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideHeight NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterX NS_AVAILABLE_IOS(11.0);
    @property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterY NS_AVAILABLE_IOS(11.0);

因为我们工程是iOS8支持的.所以我使用的是mas_topLayoutGuide和mas_bottomLayoutGuide.在iOS11上跑的效果和safe area一个样.就不需要进行系统版本的判断了.

猜你喜欢

转载自blog.csdn.net/qq_18683985/article/details/80810498
今日推荐