scrollview的幾個關鍵屬性
contentSize: 就是scrollView可以滾動的區(qū)域. 例如 frame = (0,0, 320, 480)
, contentSize = (320, 960)
, 代表scrollView可以上下滾動, 滾動區(qū)域為frame大小的兩倍.
contentOffset 是scrollview當前顯示區(qū)域頂點相對于frame頂點的偏移量, 比如上一個例子中, 將scrollView拉到最下面, 那么contentOffset = (0, 480)
, 也就是 y 偏移了480.
contentInset是scrollView中的contentView.frame.origin
和scrollView.frame.origin
之間的關系. 例如contentView.frame = (0, 30, 320, 480)
, 那么contentInset = (0, 30)
iOS11 新加入的屬性 adjustedContentInset
: 表示scrollView的內(nèi)容的自適應內(nèi)邊距, 它的取值和contentInsetAdjustmentBehavior
枚舉屬性息息相關, 同時取消vc.automaticallyAdjustsScrollViewInsets
屬性值.
scrollView關鍵屬性的實踐(一)
vc在navigationController中, navigationBar是默認狀態(tài), 并且self.automaticallyAdjustsScrollViewInsets = YES
和scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
都是使用的默認值.(注意如果當前vc沒有在navigationController或者TabbarController中時候, 結果可能不一樣, 我們一般討論的是scrollView在navigationController中的情況)
-(void)viewDidLoad{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scrollView];
scrollView.frame = self.view.bounds;
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
self.scrollView = scrollView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[scrollView addSubview:button];
button.frame = CGRectMake(0, 0, 100, 100);
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(buttonDidClick) forControlEvents:UIControlEventTouchUpInside];
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
} else {
self.automaticallyAdjustsScrollViewInsets = YES;
}
}
在iOS8上:
2018-09-27 14:49:13.103 scrollViewInVcDemo[56036:1691899] contentInset: {64, 0, 0, 0}
2018-09-27 14:49:13.103 scrollViewInVcDemo[56036:1691899] contentOffset: {0, -64}
在iOS11上:
2018-09-27 14:47:23.731497+0800 scrollViewInVcDemo[55894:1682631] contentInset: {0, 0, 0, 0}
2018-09-27 14:47:23.731605+0800 scrollViewInVcDemo[55894:1682631] contentOffset: {0, -64}
2018-09-27 14:47:23.731708+0800 scrollViewInVcDemo[55894:1682631] safeArea: {64, 0, 0, 0}
2018-09-27 14:47:23.731867+0800 scrollViewInVcDemo[55894:1682631] adjustedContentInset: {64, 0, 0, 0}
最終界面展示是一致的, 正方形的button 位于界面的左上方, navBar下:
上述代碼中, 如果設置self.navigationController.navigationBar.hidden = YES;
, 得到的結果如下:
iOS8:
2018-09-27 15:16:04.581 scrollViewInVcDemo[57368:1792003] contentInset: {20, 0, 0, 0}
2018-09-27 15:16:04.581 scrollViewInVcDemo[57368:1792003] contentOffset: {0, -20}
iOS11:
2018-09-27 15:16:56.342570+0800 scrollViewInVcDemo[57444:1799639] contentInset: {0, 0, 0, 0}
2018-09-27 15:16:56.342685+0800 scrollViewInVcDemo[57444:1799639] contentOffset: {0, -20}
2018-09-27 15:16:56.342787+0800 scrollViewInVcDemo[57444:1799639] safeArea: {20, 0, 0, 0}
2018-09-27 15:16:56.342894+0800 scrollViewInVcDemo[57444:1799639] adjustedContentInset: {20, 0, 0, 0}
可以得出結論:
- iOS10以前通過設置
self.automaticallyAdjustsScrollViewInsets = YES;
可以改變scrollView的contentInset
, 從而使得scrollView上的元素不會被navigationBar或者tabbar遮擋. - 在iOS11以后.則通過設置
contentInsetAdjustmentBehavior
屬性來改變adjustedContentInset
屬性, 來完成相同效果, 同時contentInset
屬性并未修改.并且automaticallyAdjustsScrollViewInsets
屬性失效.
scrollView關鍵屬性的實踐(二)
如果我們設置如下關鍵屬性:
self.navigationController.navigationBar.hidden = NO;
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
得到的結論如下:
/**
iOS 8:
2018-10-08 16:30:31.331 scrollViewInVcDemo[3018:165133] scrollView: <UIScrollView: 0x7fddcec31390; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fddcec32050>; layer = <CALayer: 0x7fddcec318c0>; contentOffset: {0, 0}; contentSize: {375, 1334}>
2018-10-08 16:30:31.331 scrollViewInVcDemo[3018:165133] contentInset: {0, 0, 0, 0}
2018-10-08 16:30:31.332 scrollViewInVcDemo[3018:165133] contentOffset: {0, 0}
iOS 11:
2018-10-08 16:36:40.084890+0800 scrollViewInVcDemo[3252:188595] scrollView: <UIScrollView: 0x7ffd2d837000; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x60000097a8b0>; layer = <CALayer: 0x6000007447a0>; contentOffset: {0, 0}; contentSize: {375, 1334}; adjustedContentInset: {0, 0, 0, 0}>
2018-10-08 16:36:40.085049+0800 scrollViewInVcDemo[3252:188595] contentInset: {0, 0, 0, 0}
2018-10-08 16:36:40.085251+0800 scrollViewInVcDemo[3252:188595] contentOffset: {0, 0}
2018-10-08 16:36:40.085379+0800 scrollViewInVcDemo[3252:188595] safeArea: {64, 0, 0, 0}
2018-10-08 16:36:40.085521+0800 scrollViewInVcDemo[3252:188595] adjustedContentInset: {0, 0, 0, 0}
*/
兩者結果都如下圖:
可以得出結論:
iOS10以前通過設置
self.automaticallyAdjustsScrollViewInsets = NO
;與在iOS11以后設置contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever
屬性是等效的.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever
屬性時候會有如下關系:adjustedContentInset == contentInset
iOS11以后, contentInsetAdjustmentBehavior
與adjustedContentInset
的關系
- UIScrollViewContentInsetAdjustmentAutomatic:
如果scrollview在一個automaticallyAdjustsScrollViewContentInset = YES
的controller上仇冯,并且這個Controller包含在一個navigationController中咒循,這種情況下會設置在top & bottom上adjustedContentInset = safeAreaInset + contentInset
(不管是否滾動)。其他情況下與UIScrollViewContentInsetAdjustmentScrollableAxes相同. - UIScrollViewContentInsetAdjustmentScrollableAxes:
在可滾動方向上adjustedContentInset = safeAreaInset + contentInset
埃撵,在不可滾動方向上adjustedContentInset = contentInset
;依賴于scrollEnabled和alwaysBounceHorizontal / vertical = YES卧晓,scrollEnabled默認為yes贞远,所以大多數(shù)情況下,計算方式還是adjustedContentInset = safeAreaInset + contentInset. - UIScrollViewContentInsetAdjustmentNever:
adjustedContentInset = contentInset
- UIScrollViewContentInsetAdjustmentAlways:
adjustedContentInset = safeAreaInset + contentInset
iOS中的安全區(qū)與additionalSafeAreaInsets
在http://www.reibang.com/p/efbc8619d56b中提到:
在iOS 11以后 Controller的automaticallyAdjustsScrollViewInsets屬性被廢棄了瓢捉,所以當tableView超出安全區(qū)域時系統(tǒng)自動調(diào)整了SafeAreaInsets值频丘,進而影響adjustedContentInset值,在iOS 11中決定tableView的內(nèi)容與邊緣距離的是adjustedContentInset屬性泡态,而不是contentInset搂漠。adjustedContentInset的計算方式見本文第二部分內(nèi)容。因為系統(tǒng)對adjustedContentInset值進行了調(diào)整某弦,所以導致tableView的內(nèi)容到邊緣的距離發(fā)生了變化桐汤,導致tableView下移了20pt(statusbar高度)或64pt(navigationbar高度)。
如果你的APP中使用的是自定義的navigationbar靶壮,隱藏掉系統(tǒng)的navigationbar怔毛,并且tableView的frame為(0,0,SCREEN_WIDTH, SCREEN_HEIGHT)開始,那么系統(tǒng)會自動調(diào)整SafeAreaInsets值為(20,0,0,0)亮钦,如果使用了系統(tǒng)的navigationbar馆截,那么SafeAreaInsets值為(64,0,0,0),如果也使用了系統(tǒng)的tabbar蜂莉,那么SafeAreaInsets值為(64,0,49,0)蜡娶。關于什么情況下會發(fā)生內(nèi)容下移的問題,本文第三部分有介紹映穗。
在iOS7中Apple引入的UIViewController的topLayoutGuide
和bottomLayoutGuide
屬性, 他們可以讓我們創(chuàng)建約束已避免內(nèi)容被UIKit的橫條, 狀態(tài)欄, 導航或者標簽欄覆蓋, 現(xiàn)在在iOS11中全部被被廢棄, 并且由安全區(qū)
替代.
viewController在iOS11中添加了一個新的屬性additionalSafeAreaInsets
, 通過該屬性, 我們可以規(guī)定我們自己的safe ares insets, 最終系統(tǒng)根據(jù)我們設置的additionalSafeAreaInsets
以及 adjustedContentInset
, 系統(tǒng)會計算出最終的 safe area.
注意: 如果viewController是在
navigationController
中, 那么應該先修改NavigationController的additionalSafeAreaInsets
, 然后再改變viewController
的additionalSafeAreaInsets
.
建議仔細讀一下下面的參考資料, 騰訊 bugly 寫的文章非常棒