viewController布局原理
自 iOS 7 以后苹果开始默认对ViewController 采用全屏布局,全屏布局的意思就是ViewController 的 layout 是填满整个屏幕的,这在 ViewController界面中有 StatusBar(20)、NavigationBar(44)、TabBar(49)、ToolBar(44) 的时候与以往的布局方式有着明显的差异。全屏布局的情况下,ViewController 的 layout(self.view) 会被盖在这些 Bar 下面。

伴随着全屏布局,iOS 7 以后 View Controller 添加了几个相关的属性来方便我们来做页面布局:
@property (nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
@property (nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0); // Defaults to NO, but bars are translucent by default on 7_0.
@property (nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES
- edgesForExtendedLayout
通过edgesForExtendedLayout这个属性来设置你的 View Controller 页面的哪一侧会延伸至屏幕边缘。可选值为:
UIRectEdgeNone | UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight | UIRectEdgeAll
默认值是 UIRectEdgeAll,即 View Controller 的布局是全屏填满。
在有NavigationBar、TabBar、TooBar 等系统 Bar 的情况下,该属性设置为 UIRectEdgeAll 时,ViewController 的页面会有一部分会被这些 Bar 盖住部分;而当该属性设为 UIRectEdgeNone 的时候,ViewController 的页面布局会限制在这些 Bar 之间。
extendedLayoutIncludesOpaqueBars
这个属性是对edgesForExtendedLayout属性的补充。
它的意思当NavigationBar、TabBar、TooBar这些 Bar 不是半透明时(Bar 的 translucent 属性值为 NO),如果设置extendedLayoutIncludesOpaqueBars为NO,则不会将ViewController的页面布局延伸至全屏,如果设置为YES,则坚持延伸至全屏。
从 iOS 7 开始,NavigationBar、TabBar、TooBar这些 Bar 默认都是半透明的,这时这个属性不会起到效果。automaticallyAdjustsScrollViewInsets
当我们采用全屏布局设置了edgesForExtendedLayout为UIRectEdgeAll,而此时ViewController的self.view的第一个Subview是UIScrollView类型或其子类型(如:UITableView 等)时,automaticallyAdjustsScrollViewInsets
这个属性就会被用来辅助我们对UIScrollView类的视图进行布局。automaticallyAdjustsScrollViewInsets默认值即为 YES。
拿UITableView来举例,你希望你的UITableView的内容从NavigationBar底部开始展示(因为不这样的话就会被 NavigationBar 遮住一部分),同时还需要在滑动时,UITableView 的布局又能填满全屏。这时你只需要设置automaticallyAdjustsScrollViewInsets为YES即可,系统会帮你调整UITableView的contentInset来实现效果使其中的内容不会被NavigationBar、TabBar、ToolBar挡住。你可以同样在- (void)viewWillLayoutSubviews观察UITableView的contentInset的值。
基础介绍
IOS的界面分为状态栏和导航栏,状态栏是指显示电池、时间的最顶部的一个窄条,高度为20个点;而导航栏是紧接着状态栏的44个点高度的横条,一般用于显示app标题,返回按钮等操作按钮。
在ios7之前,状态栏和导航栏是分开的,而从ios7开始状态栏和导航栏交织在一起了,状态栏变为透明,导航栏的高度变为44+20=64:
- 全局状态栏的设置
//设置状态栏的字体颜色模式
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//设置状态栏是否隐藏
[[UIApplication sharedApplication] setStatusBarHidden:YES];
我们不能随意的对状态栏的字体和颜色任意控制。只能设置两种样式。UIStatusBarStyleDefault和UIStatusBarStyleLightContent。
- 分页面的状态栏的设置
分页面的情况下有两种情况:
- 当VC属于NavigationController的时候,在VC中添加方法
-(UIStatusBarStyle)preferredStatusBarStyle
{
//返回白色
return UIStatusBarStyleLightContent;
//返回黑色
//return UIStatusBarStyleDefault;
}
// 隐藏状态栏
-(BOOL)prefersStatusBarHidden {
return YES;
}
为了保险起见,在view加载的某个阶段比如viewWillAppear中执行
[self setNeedsStatusBarAppearanceUpdate];
- 当VC属于NavigationController的时候
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
