automaticallyAdjustsScrollViewInsets屬性

在iOS7.0以后,相對(duì)于ScrollView新增屬性,默認(rèn)為YES,系統(tǒng)會(huì)根據(jù)所在界面的astatus bar, search bar, navigation bar, toolbar, or tab bar等自動(dòng)調(diào)整ScrollView的inset.
正是由于這一屬性,在添加ScrollView時(shí)會(huì)有意想不到的"驚喜".首先,調(diào)整ScrollView的inset, ScrollView的frame并沒有變化,而是其內(nèi)容的位置有變化,以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];
}

此時(shí)運(yùn)行結(jié)果如實(shí)例1,可以看到tableView的cell相對(duì)于tableView的根視圖向下調(diào)整了一個(gè)navigation bar的高度,而tableView的原點(diǎn)在左上角(透過navigation bar可以看到橙色);
實(shí)例1


實(shí)例1.png

  同理,當(dāng)tableView.frame.origin.y的值不是0的時(shí)候,根據(jù)y值的不同,得到的視圖會(huì)有不同的效果.通過測(cè)試,當(dāng)tableView.frame.origin.y的大于0的時(shí)候,tableView的cell會(huì)相對(duì)的向下移動(dòng),反之亦然;
  如果添加的ScrollView的高度比較小,甚至小于navigation bar的高度的時(shí)候, ScrollView添加到其根視圖了,但是其內(nèi)容只能顯示一部分甚至完全看不到不見,查看層級(jí)視圖的clipped content才發(fā)現(xiàn)內(nèi)容在ScrollView下面,這就是這個(gè)屬性的功勞.

但是要注意:這種自動(dòng)調(diào)整是在ScrollView是其根視圖添加的的第一個(gè)控件的時(shí)候,才會(huì)出現(xiàn)自動(dòng)調(diào)整的效果,如果在添加ScrollView之前添加了其他控件,不論控件的frame,自動(dòng)調(diào)整都會(huì)失效.例如當(dāng)代碼如下時(shí)
- (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];
}

實(shí)例圖如下


實(shí)例2.png

  
  總結(jié),automaticallyAdjustsScrollViewInsets屬性的自動(dòng)調(diào)整,實(shí)際效果是調(diào)整ScrollView的內(nèi)容的y的值,而且當(dāng)ScrollView不是其根視圖添加的第一個(gè)控件的時(shí)候,這個(gè)屬性的修飾效果會(huì)"失效";
附官方文檔

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,221評(píng)論 4 61
  • 當(dāng)頁(yè)面中同時(shí)有navigationBar和tabbar時(shí),tableView向上滑到底部的時(shí)候則會(huì)出現(xiàn)一段空白,但...
    Gary_Kwok閱讀 482評(píng)論 0 0
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,727評(píng)論 1 14
  • 光陰的路口,有些風(fēng)景還來(lái)不及觀看,歲月已經(jīng)輾轉(zhuǎn)了一程又一程。 歲末年終,一個(gè)學(xué)期轟然而過,又到寒假時(shí)。周五,開完散...
    旋非子閱讀 487評(píng)論 1 1
  • 如果有來(lái)生,要做一棵樹 站成永恒。沒有悲歡的姿勢(shì) 一半在塵土里安詳 一半在風(fēng)里飛揚(yáng) 一半灑落蔭涼 一半沐浴陽(yáng)光 非...
    憶君昔閱讀 625評(píng)論 0 0

友情鏈接更多精彩內(nèi)容