从iOS7开始,view controllers默认使用全屏布局(full-screen layout)。同时引进了不少属性,使你能更自由地控制view controllers如何布局views。这些属性是:
edgesForExtendedLayout
通过设置此属性,你可以指定view的边(上、下、左、右)延伸到整个屏幕。
typedef enum : NSUInteger {
UIRectEdgeNone = 0,
UIRectEdgeTop = 1 << 0,
UIRectEdgeLeft = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
} UIRectEdge;
edgesForExtendedLayout属性是enum类型UIRectEdge。默认值是UIRectEdgeAll, 意味着view会被拓展到整个屏幕。比如,当你把一个UIViewControllerpush到一个UINavigationController上:
UIViewController *viewController = [[UIViewController alloc] init]; viewController.view.backgroundColor = [UIColor redColor];
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
结果是这样的:
edgesForExtendedLayout = YES比你所见,经色背景延伸到了navigation bar和status bar(延伸到整个屏幕)。
接下来,如果你把值设为UIRectEdgeNone, 也就是不让view延伸到整个屏幕:
UIViewController *viewController = [[UIViewController alloc] init]; viewController.view.backgroundColor = [UIColor redColor];
viewController.edgesForExtendedLayout = UIRectEdgeNone;
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
结果是这样的:
edgesForExtendedLayout = NO关于这个属性,值得一提的是:它只有当viewController被嵌到别的container view controller中时才会起作用
如苹果官方所言:
This property is applied only to view controllers that are embedded in a container such as UINavigationController. The window’s root view controller does not react to this property. The default value of this property is UIRectEdgeAll.
automaticallyAdjustsScrollViewInsets
当你的view是UIScrollerView或其子(UITableView)时, 这个属性就派上用场了。你想让你的table从navigation bar的底部开始(否则navigation bar挡住了table上部分),同时又希望滚动时,table延伸到整个屏幕(behind the navigation bar)。此时若用edgesForExtendedLayout,滚动时无法满足要求,因为table始终从navigation bar底部开始,滚动时不会behind it。
automaticallyAdjustsScrollViewInsets就能很好地满足需求,设置些属性值为YES(也是默认值),viewController会table顶部添加inset,所以table会出现在navigation bar的底部。但是滚动时又能覆盖整个屏幕:
当设在NO时:
automaticallyAdjustsScrollViewInsets = NO无论是YES or NO,滚支时table都是覆盖整个屏幕的,但是YES时,table不会被navigation bar挡住。
从苹果的官方文档可知,不只是navigation bar,status bar, search bar, navigation bar, toolbar, or tab bar都有类似的效果。
The default value of this property is
YES
, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property toNO
if your view controller implementation manages its own scroll view inset adjustments.
extendedLayoutIncludesOpaqueBars
extendedLayoutIncludesOpaqueBars是前面两个属性的补充。如果status bar是不透明的,view不会被延伸到status bar,除非extendedLayoutIncludesOpaqueBars = YES;
如果想要让你的view延伸到navigation bar(edgesForExtendedLayout to UIRectEdgeAll)并且设置此属性为NO(默认)。view就不会延伸到不透明的status bar。
网友评论