automaticallyAdjustsScrollViewInsets
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
iOS 7.0–11.0Deprecated
tvOS 9.0–11.0Deprecated
看看官方的廢氣聲明
再來看看官方對該屬性的替換說明
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); // Defaults to YES
所以需要我們使用contentInsetAdjustmentBehavior
來替換,這個(gè)屬性究竟是干什么用的?
進(jìn)而研究一下該Behavior都有哪些狀態(tài)可供選擇:
英文版:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable
UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
中文版:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, // 與.scrollableAxes相似,但為了向后兼容依然會(huì)對navigationController里的且設(shè)置或者默認(rèn)為automaticallyAdjustsScrollViewInsets = YES的scrollView來調(diào)整top和bottom contentInset,無論是否設(shè)置了scrollView的可滑動(dòng)屬性scrollable住闯。
UIScrollViewContentInsetAdjustmentScrollableAxes, // 可理解為可滑動(dòng)軸邊緣始終會(huì)被調(diào)整(例如:contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever, // contentInset 不會(huì)被調(diào)整
UIScrollViewContentInsetAdjustmentAlways, // contentInset 始終會(huì)被scrollView的safeAreaInsets來調(diào)整
}(ios(11.0),tvos(11.0))可見;
在iOS11之前,系統(tǒng)會(huì)通過動(dòng)態(tài)調(diào)整UIScrollView的contentInset屬性來實(shí)現(xiàn)視圖內(nèi)容的偏移没咙,但是在iOS11中秦驯,系統(tǒng)不再調(diào)整UIScrollView的contentInset掌动,而是通過contentInsetAdjustmentBehavior和adjustedContentInset來配合完成蝴乔。
所以遇到NavigationController被隱藏的頁面時(shí)豹储,如果內(nèi)部視圖為scrollview或其子類tableView等,iOS11下將默認(rèn)采用UIScrollViewContentInsetAdjustmentAutomatic屬性淘这,自然視圖會(huì)被下移到UIStatusBar下(如果你的布局為scrollview.top = superview.top),這時(shí)我們只需要將scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
即可巩剖。注意這里是對scrollview設(shè)置的铝穷,而iOS11之前,我們設(shè)置不要自動(dòng)偏移的行為則是對當(dāng)前的UIViewController進(jìn)行的佳魔。 self.automaticallyAdjustsScrollViewInsets = NO;
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}