前言
朋友问我设置UIScrollView的origin为(0,0)点,但是现实的效果是(0,StatusBar),就是顶部空出了一个状态栏的高度。我就告诉他添加下面代码,能解决问题。if (@available(iOS 11, *)) { self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; }
正好就一起扩展一下关于self.View 的(0,0)点的问题
接下来我们看这下面这3个属性
- extendedLayoutIncludesOpaqueBars
- edgesForExtendedLayout
- automaticallyAdjustsScrollViewInsets
1、extendedLayoutIncludesOpaqueBars
1.1如下代码:未设置导航栏的背景
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
//默认导航栏是半透明的
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
}
未设置导航栏背景打印结果 :
extendedLayoutIncludesOpaqueBars = 0
1.2 给导航栏设置背景图
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
}
设置了导航栏背景打印结果:
extendedLayoutIncludesOpaqueBars = 0
小结1:extendedLayoutIncludesOpaqueBars
默认为NO
,在导航栏为透明的时候,self.view 的(0,0)点,在屏幕的左上角,在导航栏不透明的时候,自动移到导航栏的下方,也就是(0,navigationBarHight)
1.3 导航栏未设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.extendedLayoutIncludesOpaqueBars = YES;
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
}
导航栏未设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES打印结果:
extendedLayoutIncludesOpaqueBars = 1
1.4 导航栏设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.extendedLayoutIncludesOpaqueBars = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"t"] forBarMetrics:UIBarMetricsDefault];
NSLog(@"extendedLayoutIncludesOpaqueBars = %d",self.extendedLayoutIncludesOpaqueBars);
}
导航栏设置背景图,extendedLayoutIncludesOpaqueBars 赋值为YES打印结果:
extendedLayoutIncludesOpaqueBars = 1
小结2:当设置 extendedLayoutIncludesOpaqueBars = YES
的时候,不管导航栏是否透明,self.view的(0,0)点都是屏幕的左上角,我们在开发的时候最好设置为YES ,我们就统一计算frame,方便管理
tip:底部的tabbar
也是和navigationBar
是一样的道理
2、edgesForExtendedLayout ()
2.1edgesForExtendedLayout 设置为 UIRectEdgeAll(这也是默认值)
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.edgesForExtendedLayout = UIRectEdgeAll;
}
UIRectEdgeAll
2.2 edgesForExtendedLayout 设置为 UIRectEdgeTop
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.edgesForExtendedLayout = UIRectEdgeTop;
}
UIRectEdgeTop
2.3 edgesForExtendedLayout 设置为 UIRectEdgeBottom
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.edgesForExtendedLayout = UIRectEdgeBottom;
}
UIRectEdgeBottom
2.4 edgesForExtendedLayout 设置为 UIRectEdgeNone
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
}
UIRectEdgeNone
总结:通过设置
edgesForExtendedLayout
此属性,你可以指定view的边(上、下、左、右)延伸到整个屏幕。默认为UIRectEdgeAll
3、automaticallyAdjustsScrollViewInsets
automaticallyAdjustsScrollViewInsets
automaticallyAdjustsScrollViewInsets(默认YES)
这个属性在iOS11 已经被放弃使用了,使用UIScrollView
的contentInsetAdjustmentBehavior
所以我们在使用的时候要兼容老版本,代码如下:if (@available(iOS 11, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; }
这个我就不写的Demo了
总结:开发的时候我们就设置 ,让(0,0)点在屏幕的左上角,方便计算frame
self.extendedLayoutIncludesOpaqueBars = YES
if (@available(iOS 11, *)) {
self.tableView.contentInsetAdjustmentBehavior =
UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
网友评论