Change page on UIScrollViewCreate Auto horizontal Slideshow to display array of images in ImageView - SwiftNavigate to specific page in scrollView with paging, programmaticallyUIScrollView to jump particular pageHow do I force a scroll view to go to next page?Making a UITableView scroll when text field is selectedUIScrollview with UIButtons - how to recreate springboard?scrollRectToVisible with multiple UIScrollView not working?How to filter touch events for a UIScrollView?UIScrollView and scrollRectToVisible:animated:Paging UIScrollView with peeking neighbor pagesiOS UIPageControl without UIScrollViewChanging UIScrollView Paging SensitivityUIScrollView prevents touchesBegan, touchesMoved, touchesEnded on view controllerUIScrollView behavior

Short story with a alien planet, government officials must wear exploding medallions

Avoiding the "not like other girls" trope?

What is a romance in Latin?

Reverse dictionary where values are lists

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

Little known, relatively unlikely, but scientifically plausible, apocalyptic (or near apocalyptic) events

A category-like structure without composition?

What are some good books on Machine Learning and AI like Krugman, Wells and Graddy's "Essentials of Economics"

Why no variance term in Bayesian logistic regression?

Is it logically or scientifically possible to artificially send energy to the body?

Is it inappropriate for a student to attend their mentor's dissertation defense?

Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?

How do I handle a potential work/personal life conflict as the manager of one of my friends?

Assassin's bullet with mercury

How to show a landlord what we have in savings?

Why do bosons tend to occupy the same state?

How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?

How does a predictive coding aid in lossless compression?

Why is this clock signal connected to a capacitor to gnd?

ssTTsSTtRrriinInnnnNNNIiinngg

How can saying a song's name be a copyright violation?

Is it possible to create a QR code using text?

Why didn't Boeing produce its own regional jet?

What exploit Are these user agents trying to use?



Change page on UIScrollView


Create Auto horizontal Slideshow to display array of images in ImageView - SwiftNavigate to specific page in scrollView with paging, programmaticallyUIScrollView to jump particular pageHow do I force a scroll view to go to next page?Making a UITableView scroll when text field is selectedUIScrollview with UIButtons - how to recreate springboard?scrollRectToVisible with multiple UIScrollView not working?How to filter touch events for a UIScrollView?UIScrollView and scrollRectToVisible:animated:Paging UIScrollView with peeking neighbor pagesiOS UIPageControl without UIScrollViewChanging UIScrollView Paging SensitivityUIScrollView prevents touchesBegan, touchesMoved, touchesEnded on view controllerUIScrollView behavior













54















I have a UIScrollView with 10 pages. I am able to flick between them. I also want to have 2 buttons (a back button and a next button) which when touched will go to the previous or next page. I can't seem to figure out a way to do this though. A lot of my code comes from Apple's page control sample code. Can anybody help?



Thanks










share|improve this question


























    54















    I have a UIScrollView with 10 pages. I am able to flick between them. I also want to have 2 buttons (a back button and a next button) which when touched will go to the previous or next page. I can't seem to figure out a way to do this though. A lot of my code comes from Apple's page control sample code. Can anybody help?



    Thanks










    share|improve this question
























      54












      54








      54


      11






      I have a UIScrollView with 10 pages. I am able to flick between them. I also want to have 2 buttons (a back button and a next button) which when touched will go to the previous or next page. I can't seem to figure out a way to do this though. A lot of my code comes from Apple's page control sample code. Can anybody help?



      Thanks










      share|improve this question














      I have a UIScrollView with 10 pages. I am able to flick between them. I also want to have 2 buttons (a back button and a next button) which when touched will go to the previous or next page. I can't seem to figure out a way to do this though. A lot of my code comes from Apple's page control sample code. Can anybody help?



      Thanks







      iphone uiscrollview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 18 '09 at 7:16









      johnjohn

      310247




      310247






















          11 Answers
          11






          active

          oldest

          votes


















          131














          You just tell the buttons to scroll to the page's location:



          CGRect frame = scrollView.frame;
          frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
          frame.origin.y = 0;
          [scrollView scrollRectToVisible:frame animated:YES];





          share|improve this answer























          • Perfect! Thanks for your help.

            – john
            Dec 18 '09 at 8:40











          • This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

            – Satyam
            Jul 27 '11 at 17:03






          • 3





            It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

            – Albert Renshaw
            Apr 2 '13 at 4:10











          • i used that code but how may i display image with that position.

            – Jitendra
            May 24 '13 at 9:31











          • how do you make this work when pressing the next button quickly?

            – scord
            Nov 2 '15 at 17:43


















          18














          scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);





          share|improve this answer

























          • much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

            – Ryan Dignard
            Jun 17 '15 at 19:36











          • this answer is more clear and effective than above answer.

            – hkaraoglu
            Jun 29 '16 at 11:14











          • A one liner FTW!

            – GeneCode
            Feb 21 '17 at 1:39


















          15














          Here is an implementation for Swift 4:



          func scrollToPage(page: Int, animated: Bool) 
          var frame: CGRect = self.scrollView.frame
          frame.origin.x = frame.size.width * CGFloat(page)
          frame.origin.y = 0
          self.scrollView.scrollRectToVisible(frame, animated: animated)



          and is easily invoked by calling:



          self.scrollToPage(1, animated: true)


          Edit:



          A nicer way of doing this is to support both horizontal and vertical pagination. Here is a convenient extension for that:



          extension UIScrollView 

          func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true)
          var frame: CGRect = self.frame
          frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
          frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
          self.scrollRectToVisible(frame, animated: animated ?? true)





          This creates an extension on UIScrollView where you can scroll to any page, vertical or horisontal.



          self.scrollView.scrollTo(horizontalPage: 0)
          self.scrollView.scrollTo(verticalPage: 2, animated: true)
          self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)





          share|improve this answer

























          • You are a hero Pontus!

            – Eenvincible
            Nov 16 '17 at 17:34


















          12














          scrollRectToVisible was not working for me, so I had to animate the contentOffset. This code works in swift 3.



          func scrollToPage(_ page: Int) 
          UIView.animate(withDuration: 0.3)
          self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)







          share|improve this answer























          • Great ! I was digging whole stack for this answer !

            – Mc.Lover
            Feb 18 '17 at 15:47


















          7














          For Swift 3 this is an extension that I find very convenient:



          extension UIScrollView 
          func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval)
          let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
          DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute:
          self.setContentOffset(offset, animated: animated)
          )




          And you call it like this:



          scrollView.scrollToPage(index: 1, animated: true, after: 0.5)





          share|improve this answer























          • Just out of curiosity: in what case do you use the delay?

            – Klaas
            May 8 '18 at 12:04






          • 1





            Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

            – David Hernandez
            May 9 '18 at 5:56


















          2














          You can do it this way :



          CGRect lastVisibleRect;
          CGSize contentSize = [_scrollView contentSize];
          lastVisibleRect.size.height = contentSize.height;
          lastVisibleRect.origin.y = 0.0;
          lastVisibleRect.size.width = PAGE_WIDTH;
          lastVisibleRect.origin.x = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index

          [_scrollView scrollRectToVisible:lastVisibleRect animated:NO];





          share|improve this answer
































            2














            Here is a static method in swift:



            static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) 
            var frame: CGRect = scrollView.frame
            frame.origin.x = frame.size.width * CGFloat(page);
            frame.origin.y = 0;
            scrollView.scrollRectToVisible(frame, animated: animated)






            share|improve this answer























            • Works smooth as silk! Thx.

              – Oleg Popov
              Sep 11 '15 at 13:50


















            2














            First create a UIScrollView extension like:



            extension UIScrollView 

            func setCurrentPage(position: Int)
            var frame = self.frame;
            frame.origin.x = frame.size.width * CGFloat(position)
            frame.origin.y = 0
            scrollRectToVisible(frame, animated: true)





            then just call:



            self.scrollView.setCurrentPage(position: 2) // where 2 is your desired page





            share|improve this answer






























              0














              If scrollRectToVisible doesn't work try this :



              let frame = scrollView.frame
              let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
              self.scrollView.setContentOffset(offset, animated: true)





              share|improve this answer






























                0














                Swift Solution



                func changePage(pageControl: UIPageControl) 

                let page = CGFloat(pageControl.currentPage)
                var frame = self.scrollView.frame
                frame.origin.x = frame.size.width * page
                frame.origin.y = 0
                self.scrollView.scrollRectToVisible(frame, animated: true)






                share|improve this answer






























                  -1














                  Additional to this code that mjdth added, remember to place it in the viewWillAppear or viewDidAppear.



                  CGRect frame = scrollView.frame;
                  frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                  frame.origin.y = 0;
                  [scrollView scrollRectToVisible:frame animated:YES];





                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function ()
                    StackExchange.using("externalEditor", function ()
                    StackExchange.using("snippets", function ()
                    StackExchange.snippets.init();
                    );
                    );
                    , "code-snippets");

                    StackExchange.ready(function()
                    var channelOptions =
                    tags: "".split(" "),
                    id: "1"
                    ;
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function()
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled)
                    StackExchange.using("snippets", function()
                    createEditor();
                    );

                    else
                    createEditor();

                    );

                    function createEditor()
                    StackExchange.prepareEditor(
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader:
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    ,
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    );



                    );













                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1926810%2fchange-page-on-uiscrollview%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown

























                    11 Answers
                    11






                    active

                    oldest

                    votes








                    11 Answers
                    11






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    131














                    You just tell the buttons to scroll to the page's location:



                    CGRect frame = scrollView.frame;
                    frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                    frame.origin.y = 0;
                    [scrollView scrollRectToVisible:frame animated:YES];





                    share|improve this answer























                    • Perfect! Thanks for your help.

                      – john
                      Dec 18 '09 at 8:40











                    • This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

                      – Satyam
                      Jul 27 '11 at 17:03






                    • 3





                      It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

                      – Albert Renshaw
                      Apr 2 '13 at 4:10











                    • i used that code but how may i display image with that position.

                      – Jitendra
                      May 24 '13 at 9:31











                    • how do you make this work when pressing the next button quickly?

                      – scord
                      Nov 2 '15 at 17:43















                    131














                    You just tell the buttons to scroll to the page's location:



                    CGRect frame = scrollView.frame;
                    frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                    frame.origin.y = 0;
                    [scrollView scrollRectToVisible:frame animated:YES];





                    share|improve this answer























                    • Perfect! Thanks for your help.

                      – john
                      Dec 18 '09 at 8:40











                    • This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

                      – Satyam
                      Jul 27 '11 at 17:03






                    • 3





                      It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

                      – Albert Renshaw
                      Apr 2 '13 at 4:10











                    • i used that code but how may i display image with that position.

                      – Jitendra
                      May 24 '13 at 9:31











                    • how do you make this work when pressing the next button quickly?

                      – scord
                      Nov 2 '15 at 17:43













                    131












                    131








                    131







                    You just tell the buttons to scroll to the page's location:



                    CGRect frame = scrollView.frame;
                    frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                    frame.origin.y = 0;
                    [scrollView scrollRectToVisible:frame animated:YES];





                    share|improve this answer













                    You just tell the buttons to scroll to the page's location:



                    CGRect frame = scrollView.frame;
                    frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                    frame.origin.y = 0;
                    [scrollView scrollRectToVisible:frame animated:YES];






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 18 '09 at 7:23









                    mjdthmjdth

                    5,56543344




                    5,56543344












                    • Perfect! Thanks for your help.

                      – john
                      Dec 18 '09 at 8:40











                    • This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

                      – Satyam
                      Jul 27 '11 at 17:03






                    • 3





                      It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

                      – Albert Renshaw
                      Apr 2 '13 at 4:10











                    • i used that code but how may i display image with that position.

                      – Jitendra
                      May 24 '13 at 9:31











                    • how do you make this work when pressing the next button quickly?

                      – scord
                      Nov 2 '15 at 17:43

















                    • Perfect! Thanks for your help.

                      – john
                      Dec 18 '09 at 8:40











                    • This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

                      – Satyam
                      Jul 27 '11 at 17:03






                    • 3





                      It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

                      – Albert Renshaw
                      Apr 2 '13 at 4:10











                    • i used that code but how may i display image with that position.

                      – Jitendra
                      May 24 '13 at 9:31











                    • how do you make this work when pressing the next button quickly?

                      – scord
                      Nov 2 '15 at 17:43
















                    Perfect! Thanks for your help.

                    – john
                    Dec 18 '09 at 8:40





                    Perfect! Thanks for your help.

                    – john
                    Dec 18 '09 at 8:40













                    This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

                    – Satyam
                    Jul 27 '11 at 17:03





                    This is perfect code. I'm adding two images in each page of scroll view. In last page, i've only one image. How to handle this situation to scroll to last view?

                    – Satyam
                    Jul 27 '11 at 17:03




                    3




                    3





                    It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

                    – Albert Renshaw
                    Apr 2 '13 at 4:10





                    It should be noted that pageNumberYouWantToGoTo starts with page zero not with page one. And the last page you have would then be one less than what it really is. (e.g. to go to page 7 you would enter 6 in the code)

                    – Albert Renshaw
                    Apr 2 '13 at 4:10













                    i used that code but how may i display image with that position.

                    – Jitendra
                    May 24 '13 at 9:31





                    i used that code but how may i display image with that position.

                    – Jitendra
                    May 24 '13 at 9:31













                    how do you make this work when pressing the next button quickly?

                    – scord
                    Nov 2 '15 at 17:43





                    how do you make this work when pressing the next button quickly?

                    – scord
                    Nov 2 '15 at 17:43













                    18














                    scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);





                    share|improve this answer

























                    • much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

                      – Ryan Dignard
                      Jun 17 '15 at 19:36











                    • this answer is more clear and effective than above answer.

                      – hkaraoglu
                      Jun 29 '16 at 11:14











                    • A one liner FTW!

                      – GeneCode
                      Feb 21 '17 at 1:39















                    18














                    scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);





                    share|improve this answer

























                    • much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

                      – Ryan Dignard
                      Jun 17 '15 at 19:36











                    • this answer is more clear and effective than above answer.

                      – hkaraoglu
                      Jun 29 '16 at 11:14











                    • A one liner FTW!

                      – GeneCode
                      Feb 21 '17 at 1:39













                    18












                    18








                    18







                    scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);





                    share|improve this answer















                    scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 2 '15 at 15:25









                    rebello95

                    6,45843760




                    6,45843760










                    answered Jan 8 '14 at 11:42









                    Mr.Guru.PatelMr.Guru.Patel

                    18113




                    18113












                    • much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

                      – Ryan Dignard
                      Jun 17 '15 at 19:36











                    • this answer is more clear and effective than above answer.

                      – hkaraoglu
                      Jun 29 '16 at 11:14











                    • A one liner FTW!

                      – GeneCode
                      Feb 21 '17 at 1:39

















                    • much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

                      – Ryan Dignard
                      Jun 17 '15 at 19:36











                    • this answer is more clear and effective than above answer.

                      – hkaraoglu
                      Jun 29 '16 at 11:14











                    • A one liner FTW!

                      – GeneCode
                      Feb 21 '17 at 1:39
















                    much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

                    – Ryan Dignard
                    Jun 17 '15 at 19:36





                    much more succinct than the accepted answer. Though I personally use scroll.contentOffset.y for the 0 parameter

                    – Ryan Dignard
                    Jun 17 '15 at 19:36













                    this answer is more clear and effective than above answer.

                    – hkaraoglu
                    Jun 29 '16 at 11:14





                    this answer is more clear and effective than above answer.

                    – hkaraoglu
                    Jun 29 '16 at 11:14













                    A one liner FTW!

                    – GeneCode
                    Feb 21 '17 at 1:39





                    A one liner FTW!

                    – GeneCode
                    Feb 21 '17 at 1:39











                    15














                    Here is an implementation for Swift 4:



                    func scrollToPage(page: Int, animated: Bool) 
                    var frame: CGRect = self.scrollView.frame
                    frame.origin.x = frame.size.width * CGFloat(page)
                    frame.origin.y = 0
                    self.scrollView.scrollRectToVisible(frame, animated: animated)



                    and is easily invoked by calling:



                    self.scrollToPage(1, animated: true)


                    Edit:



                    A nicer way of doing this is to support both horizontal and vertical pagination. Here is a convenient extension for that:



                    extension UIScrollView 

                    func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true)
                    var frame: CGRect = self.frame
                    frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
                    frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
                    self.scrollRectToVisible(frame, animated: animated ?? true)





                    This creates an extension on UIScrollView where you can scroll to any page, vertical or horisontal.



                    self.scrollView.scrollTo(horizontalPage: 0)
                    self.scrollView.scrollTo(verticalPage: 2, animated: true)
                    self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)





                    share|improve this answer

























                    • You are a hero Pontus!

                      – Eenvincible
                      Nov 16 '17 at 17:34















                    15














                    Here is an implementation for Swift 4:



                    func scrollToPage(page: Int, animated: Bool) 
                    var frame: CGRect = self.scrollView.frame
                    frame.origin.x = frame.size.width * CGFloat(page)
                    frame.origin.y = 0
                    self.scrollView.scrollRectToVisible(frame, animated: animated)



                    and is easily invoked by calling:



                    self.scrollToPage(1, animated: true)


                    Edit:



                    A nicer way of doing this is to support both horizontal and vertical pagination. Here is a convenient extension for that:



                    extension UIScrollView 

                    func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true)
                    var frame: CGRect = self.frame
                    frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
                    frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
                    self.scrollRectToVisible(frame, animated: animated ?? true)





                    This creates an extension on UIScrollView where you can scroll to any page, vertical or horisontal.



                    self.scrollView.scrollTo(horizontalPage: 0)
                    self.scrollView.scrollTo(verticalPage: 2, animated: true)
                    self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)





                    share|improve this answer

























                    • You are a hero Pontus!

                      – Eenvincible
                      Nov 16 '17 at 17:34













                    15












                    15








                    15







                    Here is an implementation for Swift 4:



                    func scrollToPage(page: Int, animated: Bool) 
                    var frame: CGRect = self.scrollView.frame
                    frame.origin.x = frame.size.width * CGFloat(page)
                    frame.origin.y = 0
                    self.scrollView.scrollRectToVisible(frame, animated: animated)



                    and is easily invoked by calling:



                    self.scrollToPage(1, animated: true)


                    Edit:



                    A nicer way of doing this is to support both horizontal and vertical pagination. Here is a convenient extension for that:



                    extension UIScrollView 

                    func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true)
                    var frame: CGRect = self.frame
                    frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
                    frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
                    self.scrollRectToVisible(frame, animated: animated ?? true)





                    This creates an extension on UIScrollView where you can scroll to any page, vertical or horisontal.



                    self.scrollView.scrollTo(horizontalPage: 0)
                    self.scrollView.scrollTo(verticalPage: 2, animated: true)
                    self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)





                    share|improve this answer















                    Here is an implementation for Swift 4:



                    func scrollToPage(page: Int, animated: Bool) 
                    var frame: CGRect = self.scrollView.frame
                    frame.origin.x = frame.size.width * CGFloat(page)
                    frame.origin.y = 0
                    self.scrollView.scrollRectToVisible(frame, animated: animated)



                    and is easily invoked by calling:



                    self.scrollToPage(1, animated: true)


                    Edit:



                    A nicer way of doing this is to support both horizontal and vertical pagination. Here is a convenient extension for that:



                    extension UIScrollView 

                    func scrollTo(horizontalPage: Int? = 0, verticalPage: Int? = 0, animated: Bool? = true)
                    var frame: CGRect = self.frame
                    frame.origin.x = frame.size.width * CGFloat(horizontalPage ?? 0)
                    frame.origin.y = frame.size.width * CGFloat(verticalPage ?? 0)
                    self.scrollRectToVisible(frame, animated: animated ?? true)





                    This creates an extension on UIScrollView where you can scroll to any page, vertical or horisontal.



                    self.scrollView.scrollTo(horizontalPage: 0)
                    self.scrollView.scrollTo(verticalPage: 2, animated: true)
                    self.scrollView.scrollTo(horizontalPage: 1, verticalPage: 2, animated: true)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 11 '18 at 12:25

























                    answered Sep 26 '14 at 9:43









                    PontusPontus

                    536510




                    536510












                    • You are a hero Pontus!

                      – Eenvincible
                      Nov 16 '17 at 17:34

















                    • You are a hero Pontus!

                      – Eenvincible
                      Nov 16 '17 at 17:34
















                    You are a hero Pontus!

                    – Eenvincible
                    Nov 16 '17 at 17:34





                    You are a hero Pontus!

                    – Eenvincible
                    Nov 16 '17 at 17:34











                    12














                    scrollRectToVisible was not working for me, so I had to animate the contentOffset. This code works in swift 3.



                    func scrollToPage(_ page: Int) 
                    UIView.animate(withDuration: 0.3)
                    self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)







                    share|improve this answer























                    • Great ! I was digging whole stack for this answer !

                      – Mc.Lover
                      Feb 18 '17 at 15:47















                    12














                    scrollRectToVisible was not working for me, so I had to animate the contentOffset. This code works in swift 3.



                    func scrollToPage(_ page: Int) 
                    UIView.animate(withDuration: 0.3)
                    self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)







                    share|improve this answer























                    • Great ! I was digging whole stack for this answer !

                      – Mc.Lover
                      Feb 18 '17 at 15:47













                    12












                    12








                    12







                    scrollRectToVisible was not working for me, so I had to animate the contentOffset. This code works in swift 3.



                    func scrollToPage(_ page: Int) 
                    UIView.animate(withDuration: 0.3)
                    self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)







                    share|improve this answer













                    scrollRectToVisible was not working for me, so I had to animate the contentOffset. This code works in swift 3.



                    func scrollToPage(_ page: Int) 
                    UIView.animate(withDuration: 0.3)
                    self.scrollView.contentOffset.x = self.scrollView.frame.width * CGFloat(page)








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 26 '16 at 16:07









                    Chuy47Chuy47

                    2,00212323




                    2,00212323












                    • Great ! I was digging whole stack for this answer !

                      – Mc.Lover
                      Feb 18 '17 at 15:47

















                    • Great ! I was digging whole stack for this answer !

                      – Mc.Lover
                      Feb 18 '17 at 15:47
















                    Great ! I was digging whole stack for this answer !

                    – Mc.Lover
                    Feb 18 '17 at 15:47





                    Great ! I was digging whole stack for this answer !

                    – Mc.Lover
                    Feb 18 '17 at 15:47











                    7














                    For Swift 3 this is an extension that I find very convenient:



                    extension UIScrollView 
                    func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval)
                    let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
                    DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute:
                    self.setContentOffset(offset, animated: animated)
                    )




                    And you call it like this:



                    scrollView.scrollToPage(index: 1, animated: true, after: 0.5)





                    share|improve this answer























                    • Just out of curiosity: in what case do you use the delay?

                      – Klaas
                      May 8 '18 at 12:04






                    • 1





                      Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

                      – David Hernandez
                      May 9 '18 at 5:56















                    7














                    For Swift 3 this is an extension that I find very convenient:



                    extension UIScrollView 
                    func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval)
                    let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
                    DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute:
                    self.setContentOffset(offset, animated: animated)
                    )




                    And you call it like this:



                    scrollView.scrollToPage(index: 1, animated: true, after: 0.5)





                    share|improve this answer























                    • Just out of curiosity: in what case do you use the delay?

                      – Klaas
                      May 8 '18 at 12:04






                    • 1





                      Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

                      – David Hernandez
                      May 9 '18 at 5:56













                    7












                    7








                    7







                    For Swift 3 this is an extension that I find very convenient:



                    extension UIScrollView 
                    func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval)
                    let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
                    DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute:
                    self.setContentOffset(offset, animated: animated)
                    )




                    And you call it like this:



                    scrollView.scrollToPage(index: 1, animated: true, after: 0.5)





                    share|improve this answer













                    For Swift 3 this is an extension that I find very convenient:



                    extension UIScrollView 
                    func scrollToPage(index: UInt8, animated: Bool, after delay: TimeInterval)
                    let offset: CGPoint = CGPoint(x: CGFloat(index) * frame.size.width, y: 0)
                    DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute:
                    self.setContentOffset(offset, animated: animated)
                    )




                    And you call it like this:



                    scrollView.scrollToPage(index: 1, animated: true, after: 0.5)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 26 '16 at 5:02









                    David HernandezDavid Hernandez

                    2,0941716




                    2,0941716












                    • Just out of curiosity: in what case do you use the delay?

                      – Klaas
                      May 8 '18 at 12:04






                    • 1





                      Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

                      – David Hernandez
                      May 9 '18 at 5:56

















                    • Just out of curiosity: in what case do you use the delay?

                      – Klaas
                      May 8 '18 at 12:04






                    • 1





                      Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

                      – David Hernandez
                      May 9 '18 at 5:56
















                    Just out of curiosity: in what case do you use the delay?

                    – Klaas
                    May 8 '18 at 12:04





                    Just out of curiosity: in what case do you use the delay?

                    – Klaas
                    May 8 '18 at 12:04




                    1




                    1





                    Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

                    – David Hernandez
                    May 9 '18 at 5:56





                    Just off the top of my head, you could use it for non-interactive paging, e.g. an autonomous sliding banner.

                    – David Hernandez
                    May 9 '18 at 5:56











                    2














                    You can do it this way :



                    CGRect lastVisibleRect;
                    CGSize contentSize = [_scrollView contentSize];
                    lastVisibleRect.size.height = contentSize.height;
                    lastVisibleRect.origin.y = 0.0;
                    lastVisibleRect.size.width = PAGE_WIDTH;
                    lastVisibleRect.origin.x = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index

                    [_scrollView scrollRectToVisible:lastVisibleRect animated:NO];





                    share|improve this answer





























                      2














                      You can do it this way :



                      CGRect lastVisibleRect;
                      CGSize contentSize = [_scrollView contentSize];
                      lastVisibleRect.size.height = contentSize.height;
                      lastVisibleRect.origin.y = 0.0;
                      lastVisibleRect.size.width = PAGE_WIDTH;
                      lastVisibleRect.origin.x = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index

                      [_scrollView scrollRectToVisible:lastVisibleRect animated:NO];





                      share|improve this answer



























                        2












                        2








                        2







                        You can do it this way :



                        CGRect lastVisibleRect;
                        CGSize contentSize = [_scrollView contentSize];
                        lastVisibleRect.size.height = contentSize.height;
                        lastVisibleRect.origin.y = 0.0;
                        lastVisibleRect.size.width = PAGE_WIDTH;
                        lastVisibleRect.origin.x = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index

                        [_scrollView scrollRectToVisible:lastVisibleRect animated:NO];





                        share|improve this answer















                        You can do it this way :



                        CGRect lastVisibleRect;
                        CGSize contentSize = [_scrollView contentSize];
                        lastVisibleRect.size.height = contentSize.height;
                        lastVisibleRect.origin.y = 0.0;
                        lastVisibleRect.size.width = PAGE_WIDTH;
                        lastVisibleRect.origin.x = contentSize.width - PAGE_WIDTH * (_totalItems - pageIndex); // total item of scrollview and your current page index

                        [_scrollView scrollRectToVisible:lastVisibleRect animated:NO];






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 26 '12 at 16:56









                        Costique

                        22.7k46976




                        22.7k46976










                        answered Feb 26 '12 at 2:03









                        HSGHSG

                        934916




                        934916





















                            2














                            Here is a static method in swift:



                            static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) 
                            var frame: CGRect = scrollView.frame
                            frame.origin.x = frame.size.width * CGFloat(page);
                            frame.origin.y = 0;
                            scrollView.scrollRectToVisible(frame, animated: animated)






                            share|improve this answer























                            • Works smooth as silk! Thx.

                              – Oleg Popov
                              Sep 11 '15 at 13:50















                            2














                            Here is a static method in swift:



                            static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) 
                            var frame: CGRect = scrollView.frame
                            frame.origin.x = frame.size.width * CGFloat(page);
                            frame.origin.y = 0;
                            scrollView.scrollRectToVisible(frame, animated: animated)






                            share|improve this answer























                            • Works smooth as silk! Thx.

                              – Oleg Popov
                              Sep 11 '15 at 13:50













                            2












                            2








                            2







                            Here is a static method in swift:



                            static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) 
                            var frame: CGRect = scrollView.frame
                            frame.origin.x = frame.size.width * CGFloat(page);
                            frame.origin.y = 0;
                            scrollView.scrollRectToVisible(frame, animated: animated)






                            share|improve this answer













                            Here is a static method in swift:



                            static func scrollToPage(scrollView: UIScrollView, page: Int, animated: Bool) 
                            var frame: CGRect = scrollView.frame
                            frame.origin.x = frame.size.width * CGFloat(page);
                            frame.origin.y = 0;
                            scrollView.scrollRectToVisible(frame, animated: animated)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 2 '15 at 21:03









                            maxhudmaxhud

                            5,779124490




                            5,779124490












                            • Works smooth as silk! Thx.

                              – Oleg Popov
                              Sep 11 '15 at 13:50

















                            • Works smooth as silk! Thx.

                              – Oleg Popov
                              Sep 11 '15 at 13:50
















                            Works smooth as silk! Thx.

                            – Oleg Popov
                            Sep 11 '15 at 13:50





                            Works smooth as silk! Thx.

                            – Oleg Popov
                            Sep 11 '15 at 13:50











                            2














                            First create a UIScrollView extension like:



                            extension UIScrollView 

                            func setCurrentPage(position: Int)
                            var frame = self.frame;
                            frame.origin.x = frame.size.width * CGFloat(position)
                            frame.origin.y = 0
                            scrollRectToVisible(frame, animated: true)





                            then just call:



                            self.scrollView.setCurrentPage(position: 2) // where 2 is your desired page





                            share|improve this answer



























                              2














                              First create a UIScrollView extension like:



                              extension UIScrollView 

                              func setCurrentPage(position: Int)
                              var frame = self.frame;
                              frame.origin.x = frame.size.width * CGFloat(position)
                              frame.origin.y = 0
                              scrollRectToVisible(frame, animated: true)





                              then just call:



                              self.scrollView.setCurrentPage(position: 2) // where 2 is your desired page





                              share|improve this answer

























                                2












                                2








                                2







                                First create a UIScrollView extension like:



                                extension UIScrollView 

                                func setCurrentPage(position: Int)
                                var frame = self.frame;
                                frame.origin.x = frame.size.width * CGFloat(position)
                                frame.origin.y = 0
                                scrollRectToVisible(frame, animated: true)





                                then just call:



                                self.scrollView.setCurrentPage(position: 2) // where 2 is your desired page





                                share|improve this answer













                                First create a UIScrollView extension like:



                                extension UIScrollView 

                                func setCurrentPage(position: Int)
                                var frame = self.frame;
                                frame.origin.x = frame.size.width * CGFloat(position)
                                frame.origin.y = 0
                                scrollRectToVisible(frame, animated: true)





                                then just call:



                                self.scrollView.setCurrentPage(position: 2) // where 2 is your desired page






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 1 '17 at 8:04









                                Zahidur Rahman FaisalZahidur Rahman Faisal

                                27547




                                27547





















                                    0














                                    If scrollRectToVisible doesn't work try this :



                                    let frame = scrollView.frame
                                    let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
                                    self.scrollView.setContentOffset(offset, animated: true)





                                    share|improve this answer



























                                      0














                                      If scrollRectToVisible doesn't work try this :



                                      let frame = scrollView.frame
                                      let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
                                      self.scrollView.setContentOffset(offset, animated: true)





                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        If scrollRectToVisible doesn't work try this :



                                        let frame = scrollView.frame
                                        let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
                                        self.scrollView.setContentOffset(offset, animated: true)





                                        share|improve this answer













                                        If scrollRectToVisible doesn't work try this :



                                        let frame = scrollView.frame
                                        let offset:CGPoint = CGPoint(x: CGFloat(sender.currentPage) * frame.size.width, y: 0)
                                        self.scrollView.setContentOffset(offset, animated: true)






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Feb 23 '16 at 16:43









                                        giLisHgiLisH

                                        629




                                        629





















                                            0














                                            Swift Solution



                                            func changePage(pageControl: UIPageControl) 

                                            let page = CGFloat(pageControl.currentPage)
                                            var frame = self.scrollView.frame
                                            frame.origin.x = frame.size.width * page
                                            frame.origin.y = 0
                                            self.scrollView.scrollRectToVisible(frame, animated: true)






                                            share|improve this answer



























                                              0














                                              Swift Solution



                                              func changePage(pageControl: UIPageControl) 

                                              let page = CGFloat(pageControl.currentPage)
                                              var frame = self.scrollView.frame
                                              frame.origin.x = frame.size.width * page
                                              frame.origin.y = 0
                                              self.scrollView.scrollRectToVisible(frame, animated: true)






                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                Swift Solution



                                                func changePage(pageControl: UIPageControl) 

                                                let page = CGFloat(pageControl.currentPage)
                                                var frame = self.scrollView.frame
                                                frame.origin.x = frame.size.width * page
                                                frame.origin.y = 0
                                                self.scrollView.scrollRectToVisible(frame, animated: true)






                                                share|improve this answer













                                                Swift Solution



                                                func changePage(pageControl: UIPageControl) 

                                                let page = CGFloat(pageControl.currentPage)
                                                var frame = self.scrollView.frame
                                                frame.origin.x = frame.size.width * page
                                                frame.origin.y = 0
                                                self.scrollView.scrollRectToVisible(frame, animated: true)







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jan 3 '18 at 9:55









                                                Sazzad Hissain KhanSazzad Hissain Khan

                                                15.6k767108




                                                15.6k767108





















                                                    -1














                                                    Additional to this code that mjdth added, remember to place it in the viewWillAppear or viewDidAppear.



                                                    CGRect frame = scrollView.frame;
                                                    frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                                                    frame.origin.y = 0;
                                                    [scrollView scrollRectToVisible:frame animated:YES];





                                                    share|improve this answer



























                                                      -1














                                                      Additional to this code that mjdth added, remember to place it in the viewWillAppear or viewDidAppear.



                                                      CGRect frame = scrollView.frame;
                                                      frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                                                      frame.origin.y = 0;
                                                      [scrollView scrollRectToVisible:frame animated:YES];





                                                      share|improve this answer

























                                                        -1












                                                        -1








                                                        -1







                                                        Additional to this code that mjdth added, remember to place it in the viewWillAppear or viewDidAppear.



                                                        CGRect frame = scrollView.frame;
                                                        frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                                                        frame.origin.y = 0;
                                                        [scrollView scrollRectToVisible:frame animated:YES];





                                                        share|improve this answer













                                                        Additional to this code that mjdth added, remember to place it in the viewWillAppear or viewDidAppear.



                                                        CGRect frame = scrollView.frame;
                                                        frame.origin.x = frame.size.width * pageNumberYouWantToGoTo;
                                                        frame.origin.y = 0;
                                                        [scrollView scrollRectToVisible:frame animated:YES];






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Jul 1 '14 at 14:34









                                                        Juan ReyesJuan Reyes

                                                        39437




                                                        39437



























                                                            draft saved

                                                            draft discarded
















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid


                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.

                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1926810%2fchange-page-on-uiscrollview%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                                            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                                            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript