您的位置:首页 > 移动开发 > IOS开发

iOS 7中的extendedLayout

2015-08-05 10:24 483 查看
参考文章:
http://stackoverflow.com/questions/18798792/explaining-difference-between-automaticallyadjustsscrollviewinsets-extendedlayo http://redth.codes/ios7-full-screen-layout/
 

iOS 7以后在ViewController里面引进了一系列属性用于管理页面布局。

 

下面是Apple官方提供的文档解释,看过之后还是觉得太过于抽象,于是用代码来实验吧。

extendedLayoutIncludesOpaqueBars
 

A Boolean value indicating whether or not the extended layout includes opaque bars.

automaticallyAdjustsScrollViewInsets
 

A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
edgesForExtendedLayout
 Property

The extended edges to use for the layout.
 
 
edgesForExtendedLayout
 
新建单个页面的项目,然后加上UINavigationController



 
把背景设置成红色,界面效果如下:



 
所以可以看到在iOS7后,View的布局是默认全屏的,Navigation Bar默认是半透明的,于是在Navigation Bar下面可以看到红色的背景。
 
 

- (void)viewDidLoad {
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
}


将edgesForExtendedLayout设置成UIRectEdgeNone,表明View是不要扩展到整个屏幕的。页面效果如下:


 
UIRectEdge是个枚举类型,其他的值通过字面意思也是非常容易理解的。

typedef enum : NSUInteger {
UIRectEdgeNone   = 0,
UIRectEdgeTop    = 1 << 0,
UIRectEdgeLeft   = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight  = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
} UIRectEdge;


 

automaticallyAdjustsScrollViewInsets
 

这个属性用于如果页面是ScrollView或者UITableView,通常我们希望ScrollView或者UITableView内容显示是在UINavigation Bar下面。
通过设置edgesForExtendedLayout = UIRectEdgeNone或者self.navigationController.navigationBar.translucent = NO;可以让view的布局从UINavigation
Bar下面开始,不过一个副作用就是当页面滑动的时候,view是没有办法占据全屏的。
automaticallyAdjustsScrollViewInsets就可以很好的完成这个需求。

self.automaticallyAdjustsScrollViewInsets = NO;




这时UITableView会被UINavigation Bar遮挡住。
 

self.automaticallyAdjustsScrollViewInsets = YES;




这时可以看到UITableView的内容会从UINavigation Bar下面开始,并且这个页面的View还是占据整个屏幕的,所以这一个属性完全搞定!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 布局