美文网首页
UIScrollView相关

UIScrollView相关

作者: nuannuan_nuan | 来源:发表于2016-06-01 10:59 被阅读3次

    前言:iOS7及其以上系统,默认的将视图的扩充区域延伸至系统通知栏顶部,也就是说默认情况下的视图控制器视图区域是全屏的,那么如果view上存在子视图并且该子视图为scrollView类型的时候,对于scrollView的布局苹果有做了些特殊处理。

    示例说明:当我们把scrollView的frame默认设置为view的bounds的时候发现,位于scrollView上的子视图基于scrollView为参考系放置,如果位于(0,0)坐标下并没有被导航栏遮盖,这是为什么呢,原因就是scrollView的两个属性值发生了变化,contentInset.top和contentOffset.y,其中contentInset.top变为了64,contentOffset.y变更为了-64,这样我们的scrollView上的视图就正好显示到了导航栏的下方而不会被遮挡。

    示例相关点解释 :首先我们了解一下contentInset和contentOffset这两个概念:
    contentInset:The distance that the content view is inset from the enclosing scroll view.

    contentOffset:The point at which the origin of the content view is offset from the origin of the scroll view.

    contentSize :The size of the content view

    即:前者为content View相对于scrollView内嵌距离,后者是content View origin相对于scrollView origin的偏移距离。所以当内嵌距离为64时,content view 的origin 相对于scrollView origin的偏移量为y:-64,所以contentOffset 为-64;

    所以,当content view的顶部内嵌距离scrollView为64时,相当于此时scrollView会展示多余的顶部64个像素的内容,这和contentOffset为-64,的下拉效果是一样的。

    注意:

    对于一些需要改变contentInset的scrollView,我们可以将contentInset的设置放置在viewController的viewDidLayoutSubViews的方法中,这样可以避免一些异常发生。

    相关文章

      网友评论

          本文标题:UIScrollView相关

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