Get current page

In my scroll view, I want the current page to appear (maybe the page is not the correct terminology). I can not find the variable that contains this. But I think it should be somewhere somewhere, since the indicator can show which scroll view is currently being displayed.

Is this completely hidden from us or is there a way to access it?

+93
ios objective-c iphone cocoa-touch uiscrollview
Nov 09 '10 at 11:14
source share
13 answers

There is no UIScrollView property for the current page. You can calculate it with:

 int page = scrollView.contentOffset.x / scrollView.frame.size.width; 

If you want to round up or down to the nearest page, use:

 CGFloat width = scrollView.frame.size.width; NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width; 
+255
Nov 09 '10 at 11:19
source share

I highly recommend you use this code

 int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width; 

but if you use this code, your view does not have to be exactly on the page that indexOfPage gives you. This is because I also recommend that you use this code only in this method.

 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{} 

which is called when your scrollView finishes scrolling and has your page number really sharp

I recommend you configure scrollView to paged with this code

 [scrollView setPagingEnabled:YES]; 

So it should look like this.

 -(void) methodWhereYouSetYourScrollView { //set scrollView [scrollView setPagingEnabled:YES]; scrollView.delegate = self; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width; //your stuff with index } 
+30
Feb 14 '14 at 18:20
source share

In swift, I would do this in the extension:

 extension UIScrollView { var currentPage:Int{ return Int((self.contentOffset.x+(0.5*self.frame.size.width))/self.frame.width)+1 } } 

Then just call:

 scrollView.currentPage 
+27
Aug 03 '15 at 9:52
source share

As stated above, but as a category

 @interface UIScrollView (CurrentPage) -(int) currentPage; @end @implementation UIScrollView (CurrentPage) -(int) currentPage{ CGFloat pageWidth = self.frame.size.width; return floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1; } @end 
+12
Sep 25 '12 at 10:50
source share

Just divide the current offset by the page size:

 CGFloat pageNum = (int)(scrollView.contentOffset.x / scrollView.frame.size.width); 
+6
Jan 16 '12 at 23:15
source share

There are already good answers there. However, for scenarios where the content does not match exactly on the page - and, like me, you want to use the result for UIPageControl , then it is important to use the ceil function.

Take an example where I have “four and several” content pages.

I will describe this using my real life as an example. The width of the content is 3140 pixels. Based on the frame size of the collection, the page width is 728.

Thus, the number of pages is:

  3140 / 728 = 4.313 

My numberOfPages will be five. Four of which will be displayed in full, and the last of which - page five - will show that there are 0.313 contents left.

Now numberOfPages means page indexes will be 0, 1, 2, 3, and 4.

When I scroll to the right, running to the final offset, the scrollViewDidEndDecelerating method gives the final offset of X 2412.

Application of rounding calculation:

 2412 / 728 = 3.313 then rounded = 3 

This is not true. The user viewing the page using offset should be:

 Offset / Page User Is Viewing 0 0 728 1 1456 2 2184 3 2412 4 

Correct calculation using ceil :

 private func pageForOffset(offset:CGFloat, pageWidth width:CGFloat) -> Int { let page = Int(ceil(offset / width)) NSLog("\(__FUNCTION__) offset \(offset) width \(width) page \(page) ") return page } 
+6
Aug 20 '15 at 12:03
source share

Aoakenfo's solution is amazing, it is another solution combining your code with scrollViewDidScroll and PageController

 - (void)scrollViewDidScroll:(UIScrollView *)sender { if (sender == self.scroll) { int pageNum = (int)(self.scrPage.contentOffset.x / self.scrPage.frame.size.width); self.pagecontroller.currentPage =pageNum; } } 
+3
Jul 06 2018-12-12T00:
source share

In xCode 7.x swift 2.x you can:

 //MARK: - ScrollView Extensions // Get the current page number extension UIScrollView { var currentPage: Int { return Int(round(self.contentOffset.x / self.bounds.size.width)) } // If you have reversed offset (start from contentSize.width to 0) var reverseCurrentPage: Int { return Int(round((contentSize.width - self.contentOffset.x) / self.bounds.size.width))-1 } } 
+3
Feb 19 '16 at 13:56
source share

Another way :

 extension MyViewController: UIScrollViewDelegate { func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { let width = scrollView.frame.width let page = Int(round(scrollView.contentOffset.x/width)) print("CurrentPage:\(page)") } } 
+3
Sep 03 '17 at 12:36 on
source share

I extracted this from several of our applications ...

 - (NSInteger)currentPage { if (0 == self.frame.size.width) { NSLog(@"frame width == 0!"); return 0; } if (! self.currentPageValid) { _currentPage = round(self.contentOffset.x / self.frame.size.width); self.currentPageValid = YES; } return _currentPage; } 

Full source here

+2
Jun 05 '13 at 23:53
source share

For quick, I would just use this

  let width = scrollView.frame.width let page = round(scrollView.contentOffset.x/width) 

Pretty simple, it basically takes the position of scrollview x, divides it by the scroll width, then rounds it.

+2
Apr 11 '16 at 4:53 on
source share

Are you using UIPageControl? If so, this is the currentPage property. If not, I think you will need to calculate the page index from the scrollView offset.

+1
Nov 09 '10 at 11:17
source share

If anyone is looking for a way to do in C # for Xamarin

  public int CurrentIndex { get => (int)Math.Round(this.scrollView.ContentOffset.X / this.scrollView.Frame.Width); set => this.scrollView.ScrollRectToVisible(new CGRect(new CGPoint(this.scrollView.Frame.Size.Width * value, 0), this.scrollView.Frame.Size), true); } 

This getter and setter should provide you with the current page and also allow you to scroll to the specified page

0
Aug 24 '18 at 1:01
source share



All Articles