美文网首页
关于automaticallyAdjustsScrollView

关于automaticallyAdjustsScrollView

作者: wwwwwwdi | 来源:发表于2018-08-06 17:01 被阅读48次

    一.automaticallyAdjustsScrollViewInsets

    automaticallyAdjustsScrollViewInsets是在iOS7.0以后,相对于ScrollView新增属性,默认为YES,系统会根据所在界面的astatus bar, search bar, navigation bar, toolbar, or tab bar等自动调整ScrollView的内边距(inset)。

    (1)self.automaticallyAdjustsScrollViewInsets = YES;

    ScrollView的原点从(0,64)开始,即tableView的cell相对于tableView的根视图向下调整了一个navigation bar的高度,而tableView的原点在左上角。ScrollView部分不会让导航栏遮盖。(视图里面存在唯一一个UIScrollView或其子类View该属性才会生效)

    (2)self.automaticallyAdjustsScrollViewInsets = NO;

    ScrollView原点就是(0,0)开始,tableview 是从屏幕的最上边开始,也就是会被导航栏 & 状态栏覆盖

    假如视图里面存在多个个UIScrollView或其子类View或者在设置自适应后下拉刷新不能正常工作时可尝试以下方法:

    (3)ScrollView原点就是(0,64)开始

    self.automaticallyAdjustsScrollViewInsets = NO;//关掉自适应
    self.edgesForExtendedLayout = UIRectEdgeNone;//自行设置
    

    在IOS7之前,tableView的Frame的起始点是(0, 20),在IOS7之后,它的起始点变成了(0,0),这个时候如果为了让下拉刷新能正常工作,必须设置self.edgesForExtendedLayout = UIRectEdgeNone

    (这种情况适用于容器建立在UINavigationController)

    二.edgesForExtendedLayout

    指定边缘要延伸的方向

    (1)self.edgesForExtendedLayout=UIRectEdgeAll

    四周边缘均延伸,就是说,如果即使视图中上navigationBar,下有tabBar,那么视图仍会延伸覆盖到四周的区域,即表示tableView可延伸到 navigationBar 下面,从顶部边缘(0,0)开始。

    (2)self.edgesForExtendedLayout=UIRectEdgeNone

    默认是self.edgesForExtendedLayout=UIRectEdgeNone;即tableView从(0,64)处开始。

    三.automaticallyAdjustsScrollViewInset=YESedgesForExtendedLayout=UIRectEdgeNone的区别

    automaticallyAdjustsScrollViewInset=YES,tableView在滑动时会经过导航栏和状态栏的底下,而 edgesForExtendedLayout=UIRectEdgeNone不会经过,所以一般在设置了edgesForExtendedLayout=UIRectEdgeNone后做如下处理:

    self.navigationController.navigationBar.translucent = NO;

    四、iOS11之后

    @property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets API_DEPRECATED("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); // Defaults to YES
    

    可以看到官方文档中,提出的,iOS11之后,使用UIScrollView的contentInsetAdjustmentBehavior替代

    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));
    
    /* Configure the behavior of adjustedContentInset.
     Default is UIScrollViewContentInsetAdjustmentAutomatic.
     */
    //默认值为“自动”,向前兼容,当iOS11之前设置了automaticallyAdjustsScrollViewInsets = YES的时候,会适配顶部底部的inset
    @property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
    
    /* When contentInsetAdjustmentBehavior allows, UIScrollView may incorporate
     its safeAreaInsets into the adjustedContentInset.
     */
    @property(nonatomic, readonly) UIEdgeInsets adjustedContentInset API_AVAILABLE(ios(11.0),tvos(11.0));
    

    参考资料

    相关文章

      网友评论

          本文标题:关于automaticallyAdjustsScrollView

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