edgesForExtendedLayout浅淡

 
 

从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的底部。但是滚动时又能覆盖整个屏幕:


automaticallyAdjustsScrollViewInsets = YES

当设在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 to NO 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。




链接:http://www.jianshu.com/p/ca3c5a94c32b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值