Mokyz
Mokyz

Reputation: 111

A bug of UIScrollView contentOffset?

I set the property isPagingEnabled of a scrollView to true, like this:

let scrollView = UIScrollView(frame: view.bounds)
scrollView.contentSize = CGSize(width: view.bounds.width * 2, height: 0)
scrollView.delegate = self
scrollView.isPagingEnabled = true
view.addSubview(scrollView)

and delegate method:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print(scrollView.contentOffset.x)
}

When scrolling the view to the boundary, the console print, as shown in:

enter image description here

and

enter image description here

The values in the red box should not be printed out, right?

But, if setting isPagingEnabled to false, the console print is normal. Is this a bug of UIScrollView?

Upvotes: 3

Views: 3221

Answers (4)

karthikeyan
karthikeyan

Reputation: 3888

As far as i Know, Its working correctly, when you are setting contentSize to your scrollview, that will respond to their delegate at first time itself.

func scrollViewDidScroll(_ scrollView: UIScrollView)

Whenever user try to scroll or drag, the above delegate will call recursively, Here your contentSize of width is double of Your view bounds width(so there is two page, the contentOffset.x will start with 0 and current position of scroll, if it is in second page, that will give your view width * n ), and you set isPagingEnabled to true.

Check this https://developer.apple.com/reference/uikit/uiscrollview/1619404-contentoffset

The default value is CGPoint​Zero.

Actually contentOffset will tell you that current scroll position.

Upvotes: 1

Lalit kumar
Lalit kumar

Reputation: 2197

Your Content offset is showing wrong because your are using content size in viewdidload use content size in this method than print your content off set

 override func viewDidLayoutSubviews()
 {
  scrollView.contentSize = CGSize(width: view.bounds.width * 2, height: 0)
 }

Upvotes: 1

user7026160
user7026160

Reputation:

Please use frame instead of bounds .

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

 let scrollView = UIScrollView(frame: view.frame.size)
 scrollView.contentSize = CGSize(width: view.frame.size.width * 2, 
 height: 0)
 scrollView.delegate = self
 scrollView.isPagingEnabled = true
 view.addSubview(scrollView)

Upvotes: 0

Gourav Joshi
Gourav Joshi

Reputation: 2419

Whenever UIScrollView make scrolling, this method scrollViewDidScroll(_ scrollView: UIScrollView) invokes. Any of the direction if you scroll you will get these types of output.

However if you want like a pagination approach then instead of this method you can use:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        print("pageNumber = \(pageNumber)")

    }

By this method you will get the exact page no of UIScrollView.

Upvotes: 0

Related Questions