美文网首页ios11.0 问题iOS 进阶
UIScrollView在iOS11上的新变化

UIScrollView在iOS11上的新变化

作者: SevenJustin | 来源:发表于2017-09-15 10:35 被阅读2303次

    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

    看看官方的废气声明

    QQ20170915-101510.png

    再来看看官方对该属性的替换说明

    @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来替换,这个属性究竟是干什么用的?
    QQ20170915-101820.png

    进而研究一下该Behavior都有哪些状态可供选择:

    英文版:
    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相似,但为了向后兼容依然会对navigationController里的且设置或者默认为automaticallyAdjustsScrollViewInsets = YES的scrollView来调整top和bottom contentInset,无论是否设置了scrollView的可滑动属性scrollable。
        UIScrollViewContentInsetAdjustmentScrollableAxes, // 可理解为可滑动轴边缘始终会被调整(例如:contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES) 
        UIScrollViewContentInsetAdjustmentNever, // contentInset 不会被调整
        UIScrollViewContentInsetAdjustmentAlways, // contentInset 始终会被scrollView的safeAreaInsets来调整
    }(ios(11.0),tvos(11.0))可见;
    

    在iOS11之前,系统会通过动态调整UIScrollView的contentInset属性来实现视图内容的偏移,但是在iOS11中,系统不再调整UIScrollView的contentInset,而是通过contentInsetAdjustmentBehavior和adjustedContentInset来配合完成。

    所以遇到NavigationController被隐藏的页面时,如果内部视图为scrollview或其子类tableView等,iOS11下将默认采用UIScrollViewContentInsetAdjustmentAutomatic属性,自然视图会被下移到UIStatusBar下(如果你的布局为scrollview.top = superview.top),这时我们只需要将scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;即可。注意这里是对scrollview设置的,而iOS11之前,我们设置不要自动偏移的行为则是对当前的UIViewController进行的。 self.automaticallyAdjustsScrollViewInsets = NO;

    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    

    相关文章

      网友评论

        本文标题:UIScrollView在iOS11上的新变化

        本文链接:https://www.haomeiwen.com/subject/obhssxtx.html