Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are contentOffset& contentInset of ScrollView

Tags:

ios

swift

I have 2 questions about ScrollView. One is my major question, Another one is secondary confusion.

1.What exactly are contentOffset and contentInset of ScrollView? I will give more Details below.

2.I create a ScrollView, but it seems the ScrollView can't roll in any direction.Is the problem about offset? Or inset? I will give more Details below.

Details for question 1:

Sometimes contentOffset property and contentInset property seem to have lots of difference, but when I try to implement some of functions that relate to those two properties, I get confused. And I make a simple illustration here. Can you help me figure it out that which part is offset and which part is inset?

enter image description here

Details for question 2:

I use following function to add a ScrollView

 func addScrollView(){             // assign a new ScrollView to "scroll" class member              scroll = UIScrollView(frame: CGRectZero)             scroll!.translatesAutoresizingMaskIntoConstraints = false             // change it to gray color to help recognising              scroll!.backgroundColor = UIColor.grayColor()             self.view.addSubview(scroll!)              //add some constraints,[you can neglect following part]             let x = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)             self.view.addConstraint(x)              let y = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1/2, constant: 25)             self.view.addConstraint(y)              let left = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: 10)             self.view.addConstraint(left)              let upperSpacing = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 50)             self.view.addConstraint(upperSpacing)              let w = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 10)             self.view.addConstraint(w)              let h = NSLayoutConstraint(item: scroll!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: scroll!, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)             self.view.addConstraint(h)             print("add scroll view Successed")         } 

And it is not able to roll. It can only contain few things(Buttons or TextFields...) to fill its size, rather than put more things at places where we can't see.

So is the problem about setting outset & inset ? How can I set to fix this? And If I do so, how can I add things(buttons..) to the un-shown places programatically?

I hope you can tell me some things!

like image 220
Microos Avatar asked Oct 22 '15 17:10

Microos


People also ask

What is Contentinsetadjustmentbehavior?

This property specifies how the safe area insets are used to modify the content area of the scroll view. The default value of this property is UIScrollView.

What is Adjustedcontentinset?

The insets derived from the content insets and the safe area of the scroll view.

What is Contentsize of Scrollview?

The content size of a scroll view doesn't change anything about the bounds of a scroll view and therefore does not impact how a scroll view composites its subviews. Instead, the content size defines the scrollable area. By default, a scroll view's content size is a big, fat {w:0, h:0} .

What is scroll view in Swift?

UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView . A scroll view is a view with an origin that's adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the app's main window.


2 Answers

A few observations:

  • The contentOffset is where the user has currently scrolled within the scroll view. This obviously changes as the user scrolls. You don't generally change this programmatically (unless you want to programmatically scroll somewhere, e.g. have a button to scroll to the top).

  • The contentInset is how much the content is visually inset inside the scroll view (i.e. what the "margins" within the scrollview are). You generally set this once in IB or in viewDidLoad, as the scroll view is instantiated.

  • The contentSize is how big the scrollable content is. Note, with autolayout, you don't have to specify this manually, but rather is calculated by the constraints that you specified between the scroll view and its subviews (and the constraints for the subviews and between the subviews).

To get scrolling to work correctly, it is a combination of (a) the bounds of the scroll view, less any contentInset; and (b) the contentSize, as calculated for you from the constraints of the subviews.

like image 122
Rob Avatar answered Sep 20 '22 04:09

Rob


contentOffset indicates the current position of the scroll view content, relative to the origin coordinates on the top-left corner. You shouldn't programmatically set this value unless you would like to programmatically adjust the scroll position.

contentInset allows to specify margins around the content in the scrollview. You can specify the margin programmatically as follows:

scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 7.0) 
like image 32
Eneko Alonso Avatar answered Sep 22 '22 04:09

Eneko Alonso