快速自动化适配iPhone X

Masonry版本

 iOS 11之前的设置:

make.bottom.equalTo(self.view.mas_bottom);

 iOS 11之后的设置:

make.bottom.equalTo(self.view.mas_bottomMargin);
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.height.mas_equalTo(100);
       make.left.equalTo(self.leftLabel.mas_right);
       make.right.equalTo(self.view);
       if (@available(iOS 11.0, *)) {
           make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
       } else {
           make.bottom.equalTo(self.view.mas_bottom);
       }
}];

代码布局

 safeAreaInsets会给出上下左右各个方位的非安全区域的大小,可以通过这些值来设置View的位置。

@property (nonatomic,readonly) UIEdgeInsets safeAreaInsets API_AVAILABLE(ios(11.0),tvos(11.0));

 定义宏

#define IOS11_OR_LATER_SPACE(par) 
({
float space = 0.0;
if (@available(iOS 11.0, *))
space = par;
(space);
})
#define JF_KEY_WINDOW [UIApplication sharedApplication].keyWindow
#define JF_TOP_SPACE IOS11_OR_LATER_SPACE(JF_KEY_WINDOW.safeAreaInsets.top)
#define JF_TOP_ACTIVE_SPACE IOS11_OR_LATER_SPACE(MAX(0, JF_KEY_WINDOW.safeAreaInsets.top-20))
#define JF_BOTTOM_SPACE IOS11_OR_LATER_SPACE(JF_KEY_WINDOW.safeAreaInsets.bottom)

 使用方法:

- (void)createView{
   UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
   view.backgroundColor = [UIColor redColor];
   [self.view addSubview:view];
    
   self.bottomView = view;
}
- (void)viewDidLayoutSubviews{
   [super viewDidLayoutSubviews];
    
   CGRect frame = self.bottomView.frame;
   frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;
   self.bottomView.frame = frame;
}

PS:safeAreaInsets还可以用来进行设置连接于View边缘的ScrollView的Insets额外滑动区域

链接

你可能感兴趣的:(快速自动化适配iPhone X)