专栏名称: Adrenine
iOS开发
目录
相关文章推荐
51好读  ›  专栏  ›  Adrenine

布局之extendedLayout与sizeToFit

Adrenine  · 掘金  ·  · 2017-12-13 08:46

正文

iOS 7以后在ViewController里面引进了一系列属性用于管理页面布局。 extendedLayout有几个相似的参数:

edgesForExtendedLayout
automaticallyAdjustsScrollViewInsets
extendedLayoutIncludesOpaqueBars

下面是Apple官方提供的文档解释,看过之后还是觉得太过于抽象,于是用代码来实验吧。 ** edgesForExtendedLayout ** The extended edges to use for the layout. ** automaticallyAdjustsScrollViewInsets ** A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets. ** extendedLayoutIncludesOpaqueBars ** A Boolean value indicating whether or not the extended layout includes opaque bars.
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;







请到「今天看啥」查看全文