iOS7中edgesForExtendedLayout、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets的区别

来源:互联网 发布:linux服务器性能优化 编辑:程序博客网 时间:2024/04/30 02:04

原文地址:Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

从iOS7开始,view controllers默认使用的是全屏幕布局,这样通过设置相关属性,开发者可以灵活对view进行布局。下面介绍几个布局相关的属性:

1、edgesForExtendedLayout

这个属性用于设置view的某个边能够覆盖整个屏幕。设想下,你push了一个UIViewController到一个UINavigationController,这个UIViewController里面view的布局是从导航栏底部开始的,通过这个属性,你将能够设置view的哪一边(顶部、左边、右边、底部)能够延伸到整个屏幕。

看下面的例子:

UIViewController *viewController = [[UIViewController alloc] init];viewController.view.backgroundColor = [UIColor redColor];UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

在上述代码中,我们没有设置edgesForExtendedLayout的值,所以默认就是UIRectEdgeAll,viewController的view的布局延伸到了整个屏幕(可以看到导航栏、状态栏背景的红色)。

下面是上述代码的效果:

enter image description here

由此可见,红色的背景延伸到了导航栏和状态栏。

下面如果将值设为UIRectEdgeNone,这样view controller就不会将view延伸至整个屏幕了。

UIViewController *viewController = [[UIViewController alloc] init];viewController.view.backgroundColor = [UIColor redColor];viewController.edgesForExtendedLayout = UIRectEdgeNone;UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

结果如下:

enter image description here

2、automaticallyAdjustsScrollViewInsets

该属性在当你的view是UIScrollView或类似的ScrollView(比如UITableView)的时候使用。你想让tableview从导航栏的底部开始显示,否则就无法看到tableview的全部内容(部分cell将会被导航栏挡住),同时你又想在滚动屏幕的时候tableview能充满整个屏幕。此时,设置edgesForExtendedLayout=None没有效果,因为tableview会紧接导航栏开始显示,当你滚动的时候也不会隐藏到导航栏。

这个属性就是为了解决这个问题,如果你由viewcontroller自动调整insents(设置该属性为YES,也是默认设置),它会在table的顶部加上inset,这样显示的时候table就能从导航栏底部开始,而滚动的时候,又能延伸整个屏幕。

automaticallyAdjustsScrollViewInsets = NO的效果:

enter image description here

automaticallyAdjustsScrollViewInsets =  YES (默认值)的效果:

enter image description here

上面两种情况下,table视图都会滚动到导航栏后面,但是第二种情况下,table视图的显示是紧接导航栏开始的,第一种情况导航栏覆盖了一部分数据。

3、extendedLayoutIncludesOpaqueBars

这个属性是对前面的补充,如果状态栏是不透明的,view将不会延伸到状态栏,除非将该属性的值设置为YES。


So, if you extend your view to cover the navigation bar (edgesForExtendedLayout toUIRectEdgeAll) and the parameter is NO (default) it wont cover the status bar if it's opaque.

If something is not clear, write a comment and I'll answer to it.

** How iOS knows what UIScrollView to use? **iOS grabs the first subview in your viewcontroller's view, so the one at index 0, and if it's a subclass ofUIScrollView then applies the explained properties to it.

Of course, this means that UITableViewController works by default (since theUITableView is the first view).


0 0