@interface ViewController ()
@property(nonatomic,strong)UIView *safeview;
@property(nonatomic,strong)UIButton *topButton;
@end
static inline UIEdgeInsets safeArea_Inset(UIView *view) {
if (@available(iOS 11.0, *)) {
return view.safeAreaInsets;
}
return UIEdgeInsetsZero;
}
static inline CGRect zj_layoutFrame(UIView *view) {
if (@available(iOS 11.0, *)) {
return view.safeAreaLayoutGuide.layoutFrame;
}
return view.frame;
}
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//NSLog(@"viewWillAppear: %@", self);
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
-(void)viewSafeAreaInsetsDidChange{
[super viewSafeAreaInsetsDidChange];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
-(void)viewLayoutMarginsDidChange{
[super viewLayoutMarginsDidChange];
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIEdgeInsets safeAreaInsets = safeArea_Inset(self.view);
NSLog(@"%s\n %@ \n%@", __func__, NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromCGRect(zj_layoutFrame(self.view)));
[self buildlBottomLayoutGuide];
}
-(void)buildlBottomLayoutGuide {
self.topButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.topButton.backgroundColor = [UIColor orangeColor];
self.topButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.topButton addTarget:self action:@selector(ppt1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.topButton];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[topButton(100)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{} views:@{@"topButton":self.topButton}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide]-20-[topButton(100)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{} views:@{@"topButton":self.topButton,@"topLayoutGuide":self.topLayoutGuide}]];
[self.view addSubview:self.safeview];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ppt)];
[self.safeview addGestureRecognizer:tap];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[safeview(100)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{} views:@{@"safeview":self.safeview}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[safeview(100)]-20-[bottomLayoutGuide]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{} views:@{@"safeview":self.safeview,@"bottomLayoutGuide":self.bottomLayoutGuide}]];
}
-(void)ppt1 {
if (@available(iOS 11.0, *)) {
self.additionalSafeAreaInsets = UIEdgeInsetsMake(50, 0, 60, 0);
}
}
-(void)ppt{
id obc = [[NSClassFromString(@"xxx") alloc] init];
[self.navigationController pushViewController:obc animated:YES];
}
-(UIView*)safeview{
if (!_safeview) {
_safeview = [UIView new];
_safeview.translatesAutoresizingMaskIntoConstraints = NO;
_safeview.backgroundColor = [UIColor redColor];
}
return _safeview;
}
运行看到控制打印的执行顺序及 安全区域的变化:
2017-11-16 15:49:35.984279+0800 safe[58767:5415421] -[ViewController viewDidLoad]
{0, 0, 0, 0}
{{0, 0}, {375, 812}}
2017-11-16 15:49:35.986049+0800 safe[58767:5415421] -[ViewController viewWillAppear:]
{0, 0, 0, 0}
{{0, 0}, {375, 812}}
2017-11-16 15:49:35.986906+0800 safe[58767:5415421] -[ViewController viewLayoutMarginsDidChange]
{88, 0, 34, 0}
{{0, 0}, {375, 812}}
2017-11-16 15:49:35.987287+0800 safe[58767:5415421] -[ViewController viewSafeAreaInsetsDidChange]
{88, 0, 34, 0}
{{0, 88}, {375, 690}}
2017-11-16 15:49:35.988129+0800 safe[58767:5415421] -[ViewController viewWillLayoutSubviews]
{88, 0, 34, 0}
{{0, 88}, {375, 690}}
2017-11-16 15:49:35.988625+0800 safe[58767:5415421] -[ViewController viewDidLayoutSubviews]
{88, 0, 34, 0}
{{0, 88}, {375, 690}}
2017-11-16 15:49:36.046169+0800 safe[58767:5415421] -[ViewController viewDidAppear:]
{88, 0, 34, 0}
{{0, 88}, {375, 690}}
点击橙色按钮看其安全区域变化:
2017-11-16 15:55:50.658061+0800 safe[58767:5415421] -[ViewController viewLayoutMarginsDidChange]
{138, 0, 94, 0}
{{0, 88}, {375, 690}}
2017-11-16 15:55:50.658297+0800 safe[58767:5415421] -[ViewController viewSafeAreaInsetsDidChange]
{138, 0, 94, 0}
{{0, 138}, {375, 580}}
2017-11-16 15:55:50.658620+0800 safe[58767:5415421] -[ViewController viewWillLayoutSubviews]
{138, 0, 94, 0}
{{0, 138}, {375, 580}}
2017-11-16 15:55:50.658877+0800 safe[58767:5415421] -[ViewController viewDidLayoutSubviews]
{138, 0, 94, 0}
{{0, 138}, {375, 580}}