转载

iOS 11.0 iPhone X safeArea适配

  • iOS 11.0之后系统新增安全区域变化方法

UIViewController中新增:
- (void)viewSafeAreaInsetsDidChange;
UIView中新增:
- (void)viewSafeAreaInsetsDidChange;
  • 通过安全区域变化来改变视图的位置

如果屏幕旋转,相应的安全区域也会变化,所以不比担心。![safearea.gif](http://upload-

images.jianshu.io/upload_images/1186277-ab32b1be56378531.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    
    NSLog(@"viewSafeAreaInsetsDidChange-%@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets));
    
    [self updateOrientation];
}
/**
 更新屏幕safearea frame
 */
- (void)updateOrientation {
    if (@available(iOS 11.0, *)) {
        CGRect frame = self.customerView.frame;
        frame.origin.x = self.view.safeAreaInsets.left;
        frame.size.width = self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right;
        frame.size.height = self.view.frame.size.height - self.view.safeAreaInsets.bottom;
        self.customerView.frame = frame;
    } else {
        // Fallback on earlier versions
    }
}

iOS 11.0 iPhone X safeArea适配

safeArea.gif

说明:使用xib创建视图,工程适配8.0以上系统,xib inspector中未勾选Safe Aear Layout Guide选项(iOS 9.0前无Safe Area Layout Guide)。其中粉色为当前viewcontroller的view,紫色的部分属于安全区域内部分(自定义视图)。

正文到此结束
Loading...