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一个样.就不需要进行系统版本的判断了.

对于在ViewController内,使用mas_XXXLayoutGuide
对于在View内,推荐使用mas_XXXMargin

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值