(转)UIScrollView 的 contentoffset和contentinset

Take a look at the image bellow. The content view is marked using ‘#’ where as the scrollable area (example: screen) ismarked using ‘-‘:

contentOffset

As you can clearly see, the content that we want to display in the scroll viewdoesn’t entirely fit in the scrollable area so we need to scroll in order for usto view the content.
The Apple documentation describes contentOffset
as follows:var contentOffset: CGPoint
“The point at which the origin of the content view is offset from the origin of the scroll view.”
In other words it defines the point in the content view that is visible at the top left of the scroll view bounds.We can use this property to scroll programmatically .
So for example:

就是在content里找到相应offset的点,然后和scrollview的0点相对
contentOffset = CGPoint(x:0.0, y:0.0)
 
 ________________ <------ content view's y = 0
|  ############  |
|  #          #  |
|  #          #  |
|  #          #  |
|  #          #  |
 --# ---------#--  
   #          #  
   #          #  
   #          #
   #          #
   ############

contentOffset = CGPoint(x: 0.0, y: 42.0)  
   ############
   #    42    #
 __#__________#__  <------ content view's y = 42
|  #          #  |
|  #          #  |
|  #          #  |
|  #          #  |
|  #          #  |
 --#----------#--
   #          #
   ############   

contentOffset = CGPoint(x: 0.0, y: -66.0)  
 ________________  <------ content view's y = -66
|                |
|      +66       |
|  ############  | 
|  #          #  |
|  #          #  |
 --# ---------#--  
   #          #  
   #          #  
   #          #
   #          #
   #          #
   #          #
   ############ 

contentInset

The Apple documentation describes contentInset
as follows:var contentInset: UIEdgeInsets
“The distance that the content view is inset from the enclosing scroll view.”
In other words it is the distance that the content view is inset from the enclosing scroll view (padding).So, if you want to add more scrollable space at the bottom or at the top, you can use the property contentInset.topand contentInset.bottom, so you can add some extra space without changing the content size.It is important to note that through the contentInset we add more scrollable space. So an contentInset.top = 10will add an extra space of 10 at the top. One way to think of it is “how much to shift the content down the scroll viewwhen we’re scrolled all the way to the top”. When we scroll down this padding also scrolls along with the rest of the content.

contentInset = UIEdgeInsetsMake(66.0, 0.0, 0.0, 0.0)
// Scrolled all the way to the top
 ________________  
|                |
|       66       |
|  ############  | 
|  #          #  |
|  #          #  |
 --# ---------#--  
   #          #  
   #          #  
   #          #
   #          #
   #          #
   #          #
   ############ 


contentInset = UIEdgeInsetsMake(0.0, 0.0, 66.0, 0.0)
// Scrolled all the way to the bottom
   ############   
   #          #  
   #          #  
   #          # 
   #          #  
   #          #  
 __#__________#__
|  #          #  |
|  #          #  |
|  #          #  |
|  ############  |
|       66       |
 ----------------
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,329评论 0 23
  • 当遭遇逆境时,你会怎么做? 一、正视逆境 当遭遇逆境时,很多人会责怪自己,如果当初我不怎么怎么样,现在就不会这样。...
    idoixiu阅读 370评论 2 4
  • 记忆是什么? 用科学的说法,记忆就是“记”和“忆”,“记”包括识记、保持,“忆”包括回忆和再认。从公元前4世纪柏拉...
    独孤明阅读 510评论 0 1
  • 像“冥想”、“打坐”这种方法,我在高中时就有过体验。那时母亲每天在练一种《禅密功》,其中就有‘打坐’,我试着做过几...
    杰克言JACKYAN阅读 609评论 1 0

友情链接更多精彩内容