What's the UIScrollView contentInset property for?

前端 未结 5 1609
情话喂你
情话喂你 2020-11-29 15:10

Can someone explain to me what the contentInset property in a UIScrollView instance is used for? And maybe provide an example?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 15:55

    It sets the distance of the inset between the content view and the enclosing scroll view.

    Obj-C

    aScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 7.0);
    

    Swift 5.0

    aScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 7.0)
    

    Here's a good iOS Reference Library article on scroll views that has an informative screenshot (fig 1-3) - I'll replicate it via text here:

      _|←_cW_→_|_↓_
       |       | 
    ---------------
       |content| ↑
     ↑ |content| contentInset.top
    cH |content|
     ↓ |content| contentInset.bottom
       |content| ↓
    ---------------
      _|_______|___ 
                 ↑
    
    
       (cH = contentSize.height; cW = contentSize.width)
    

    The scroll view encloses the content view plus whatever padding is provided by the specified content insets.

提交回复
热议问题