前言
从iOS7开始,UIViewController默认使用全屏布局,并增加了edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets及extendedLayoutIncludesOpaqueBars这三个属性用于控制布局。
1、edgesForExtendedLayout
这个属性是默认是UIRectEdgeAll(向四周延伸的),顶部是被导航栏盖住的,底部被tabbar盖住,占满整个屏幕。
首先,我们看什么都不设置的情况
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"test";
self.view.backgroundColor = [UIColor orangeColor];
}```
![默认情况下](https://img.haomeiwen.com/i2498906/3a8fffdad0543ce8.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
然后,把edgesForExtendedLayout属性设置为UIRectEdgeNone,发现导航栏和tabbar都变灰了
-
(void)viewDidLoad {
[super viewDidLoad];self.title = @"test";
self.view.backgroundColor = [UIColor orangeColor];self.edgesForExtendedLayout = UIRectEdgeNone;
}
![UIRectEdgeNone](https://img.haomeiwen.com/i2498906/9bb7401c1961b65e.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
以上效果是在导航栏和tabbar是透明的情况下,如果把导航栏和tabbar都设置为不透明,即使edgesForExtendedLayout属性设置为UIRectEdgeAll,view也会和上图的效果一样,在导航栏和tabbar之间。代码如下:
-
(void)viewDidLoad {
[super viewDidLoad];self.title = @"test";
self.view.backgroundColor = [UIColor orangeColor];self.edgesForExtendedLayout = UIRectEdgeAll;
self.navigationController.navigationBar.translucent = NO;
self.tabBarController.tabBar.translucent = NO;
}
#2、extendedLayoutIncludesOpaqueBars
这个属性是在默认可延展的情况下,但导航栏和tabbar不透明时,view最终没有延展,这时设置extendedLayoutIncludesOpaqueBars为YES,则会是view延展到顶部和底部,这次我们分别在顶部和底部添加两个辅助视图,便于观察,首先来看下默认为NO时的效果:
-
(void)viewDidLoad {
[super viewDidLoad];self.title = @"test";
self.view.backgroundColor = [UIColor orangeColor];UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];self.navigationController.navigationBar.translucent = NO;
self.tabBarController.tabBar.translucent = NO;NSLog(@"%s%@",func,NSStringFromCGRect(self.view.frame));
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%s%@",__func__,NSStringFromCGRect(self.view.frame));
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-150, 150, 150)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
}
![打印view的frame](https://img.haomeiwen.com/i2498906/266fa8e189e8fa72.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)
由打印的结果可看出,view的大小是在viewDidAppear中才最终确定下来,所以红色的视图要在viewDidAppear中添加才更准确,效果图如下:
![extendedLayoutIncludesOpaqueBars为NO](https://img.haomeiwen.com/i2498906/3a81ddea95fffb28.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
接下来在viewDidLoad方法中加入如下代码,再看效果:
self.extendedLayoutIncludesOpaqueBars = YES;
![extendedLayoutIncludesOpaqueBars为YES](https://img.haomeiwen.com/i2498906/93070749d7cbc646.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
效果很明显,大家一看应该就明白了吧
#3、automaticallyAdjustsScrollViewInsets
该属性是在当前视图为UIScrollView或者其子类(比如UITabbleView)时,用来控制正文内容位置的属性,当automaticallyAdjustsScrollViewInsets设置为YES(默认为YES)时,正文是从导航栏底部开始的,而且因为导航栏是半透明的,当向上滚动时,是可以透过导航栏看见下面的内容的;相反,当automaticallyAdjustsScrollViewInsets设置为NO时,内容的起始位置就延展到了导航栏的下面,被遮盖住了:
![automaticallyAdjustsScrollViewInsets为YES](https://img.haomeiwen.com/i2498906/080abedca4c5c496.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
![automaticallyAdjustsScrollViewInsets为NO时](https://img.haomeiwen.com/i2498906/fdfaafcd18b904ed.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
以上两张都是初始位置,没有滚动的情况下
网友评论