-
使用安全区域的情况
当视图部分处于非安全区域内时,SafeAreaInsets会返回非0的数值,若整个视图已经处在安全区域中,则SafeAreaInsets会返回UIEdgeInsetsZero -
iOS 11.0之后系统新增安全区域改变时的回调方法
UIViewController中新增:
- (void)viewSafeAreaInsetsDidChange;
UIView中新增:
- (void)viewSafeAreaInsetsDidChange;
- 通过安全区域变化来改变视图的位置
如果屏幕旋转,相应的安全区域也会变化,所以不比担心。
- (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
}
}
safeArea.gif
说明:使用xib创建视图,工程适配8.0以上系统,xib inspector中未勾选Safe Aear Layout Guide选项(iOS 9.0前无Safe Area Layout Guide)。其中粉色为当前viewcontroller的view,紫色的部分属于安全区域内部分(自定义视图)。
- 增大安全区域
iOS 11.0还有一个属性additionalSafeAreaInsets,可以用来扩展安全区域的大小,如:
if (@available(iOS 11.0, *)) {
self.additionalSafeAreaInsets = UIEdgeInsetsMake(50, 50, 50, 50);
NSLog(@"additionalSafeAreaInsets-%@",NSStringFromUIEdgeInsets(self.additionalSafeAreaInsets));
NSLog(@"safeAreaInsets-%@",NSStringFromUIEdgeInsets(self.view.safeAreaInsets));
} else {
// Fallback on earlier versions
}
打印结果如下:
2017-11-07 10:28:04.012543+0800 XIBLayoutDemo[78269:6690668] additionalSafeAreaInsets-{50, 50, 50, 50}
2017-11-07 10:28:04.012737+0800 XIBLayoutDemo[78269:6690668] safeAreaInsets-{0, 0, 0, 0}
但在viewSafeAreaInsetsDidChange方法中监测,已经得到相应的改变:
2017-11-07 10:32:49.425656+0800 XIBLayoutDemo[78269:6690668] viewSafeAreaInsetsDidChange-{50, 50, 84, 50}
2017-11-07 10:32:49.426402+0800 XIBLayoutDemo[78269:6690668] viewSafeAreaInsetsDidChange-{94, 50, 84, 50}
2017-11-07 10:32:49.427091+0800 XIBLayoutDemo[78269:6690668] viewSafeAreaInsetsDidChange-{50, 50, 84, 50}
已经在原来{0,0,34,0}的基础上变为{50,50,84,50},上左下右各增加了50,上图变为下图所示:
iPhone X.png
- 在iOS 11中给navigationItem添加搜索框也是正确的
if (@available(iOS 11.0, *)) {
UISearchController *searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
searchVC.searchResultsUpdater = self;
searchVC.searchBar.delegate = self;
searchVC.searchBar.placeholder = @"填写搜索文字";
searchVC.searchBar.showsCancelButton = YES;
UITextField *searchField = [searchVC.searchBar valueForKey:@"_searchField"];
searchField.textColor = [UIColor redColor];
searchField.returnKeyType = UIReturnKeyGo;
self.navigationItem.searchController = searchVC;
self.navigationItem.hidesSearchBarWhenScrolling = YES;
}
进入页面日志打印如下:
2017-11-08 10:27:56.488194+0800 XIBLayoutDemo[82378:7134741] viewSafeAreaInsetsDidChange-{0, 0, 34, 0}
2017-11-08 10:27:56.488650+0800 XIBLayoutDemo[82378:7134741] viewSafeAreaInsetsDidChange-{44, 0, 34, 0}
2017-11-08 10:27:56.489282+0800 XIBLayoutDemo[82378:7134741] viewSafeAreaInsetsDidChange-{0, 0, 34, 0}
searchController.png
网友评论