automaticallyAdjustsScrollViewInsets属性

在iOS7.0以后,相对于ScrollView新增属性,默认为YES,系统会根据所在界面的astatus bar, search bar, navigation bar, toolbar, or tab bar等自动调整ScrollView的inset.
正是由于这一属性,在添加ScrollView时会有意想不到的"惊喜".首先,调整ScrollView的inset, ScrollView的frame并没有变化,而是其内容的位置有变化,以UITableView为例(演示方便),代码如下

- (void)viewDidLoad {
[super viewDidLoad];

UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds];

tableView.backgroundColor = [UIColor orangeColor];

tableView.dataSource = self;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];

[self.view addSubview:tableView];
}

此时运行结果如实例1,可以看到tableView的cell相对于tableView的根视图向下调整了一个navigation bar的高度,而tableView的原点在左上角(透过navigation bar可以看到橙色);
实例1


实例1.png

  同理,当tableView.frame.origin.y的值不是0的时候,根据y值的不同,得到的视图会有不同的效果.通过测试,当tableView.frame.origin.y的大于0的时候,tableView的cell会相对的向下移动,反之亦然;
  如果添加的ScrollView的高度比较小,甚至小于navigation bar的高度的时候, ScrollView添加到其根视图了,但是其内容只能显示一部分甚至完全看不到不见,查看层级视图的clipped content才发现内容在ScrollView下面,这就是这个属性的功劳.

但是要注意:这种自动调整是在ScrollView是其根视图添加的的第一个控件的时候,才会出现自动调整的效果,如果在添加ScrollView之前添加了其他控件,不论控件的frame,自动调整都会失效.例如当代码如下时
- (void)viewDidLoad {
[super viewDidLoad];
//添加tableView
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,100, self.view.bounds.size.width, self.view.bounds.size.height)];

tableView.backgroundColor = [UIColor orangeColor];

tableView.dataSource = self;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
//添加label
UILabel *label = [UILabel labelWithText:@"label" andTextColor:[UIColor darkGrayColor] andFontSize:18];

[label sizeToFit];

label.frame = CGRectMake(0, 0, label.bounds.size.width, label.bounds.size.height);

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,100, self.view.bounds.size.width, self.view.bounds.size.height)];

view.backgroundColor = [UIColor greenColor];

[view addSubview:label];
[view addSubview:tableView];

[self.view addSubview:view];
}

实例图如下


实例2.png

  
  总结,automaticallyAdjustsScrollViewInsets属性的自动调整,实际效果是调整ScrollView的内容的y的值,而且当ScrollView不是其根视图添加的第一个控件的时候,这个属性的修饰效果会"失效";
附官方文档

A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
Discussion
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.
Availability
Available in iOS 7.0 and deprecated in iOS 11.0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • 当页面中同时有navigationBar和tabbar时,tableView向上滑到底部的时候则会出现一段空白,但...
    Gary_Kwok阅读 452评论 0 0
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,553评论 1 14
  • 光阴的路口,有些风景还来不及观看,岁月已经辗转了一程又一程。 岁末年终,一个学期轰然而过,又到寒假时。周五,开完散...
    旋非子阅读 444评论 1 1
  • 如果有来生,要做一棵树 站成永恒。没有悲欢的姿势 一半在尘土里安详 一半在风里飞扬 一半洒落荫凉 一半沐浴阳光 非...
    忆君昔阅读 553评论 0 0