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
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
add a comment |
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
add a comment |
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
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
iphone uiscrollview
asked Dec 18 '09 at 7:16
johnjohn
310247
310247
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
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];
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 thatpageNumberYouWantToGoTo
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
|
show 1 more comment
scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
much more succinct than the accepted answer. Though I personally usescroll.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
add a comment |
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)
You are a hero Pontus!
– Eenvincible
Nov 16 '17 at 17:34
add a comment |
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)
Great ! I was digging whole stack for this answer !
– Mc.Lover
Feb 18 '17 at 15:47
add a comment |
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)
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
add a comment |
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];
add a comment |
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)
Works smooth as silk! Thx.
– Oleg Popov
Sep 11 '15 at 13:50
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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];
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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];
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 thatpageNumberYouWantToGoTo
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
|
show 1 more comment
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];
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 thatpageNumberYouWantToGoTo
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
|
show 1 more comment
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];
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];
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 thatpageNumberYouWantToGoTo
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
|
show 1 more comment
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 thatpageNumberYouWantToGoTo
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
|
show 1 more comment
scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
much more succinct than the accepted answer. Though I personally usescroll.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
add a comment |
scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
much more succinct than the accepted answer. Though I personally usescroll.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
add a comment |
scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
scroll.contentOffset = CGPointMake(scroll.frame.size.width*pageNo, 0);
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 usescroll.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
add a comment |
much more succinct than the accepted answer. Though I personally usescroll.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
add a comment |
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)
You are a hero Pontus!
– Eenvincible
Nov 16 '17 at 17:34
add a comment |
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)
You are a hero Pontus!
– Eenvincible
Nov 16 '17 at 17:34
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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)
Great ! I was digging whole stack for this answer !
– Mc.Lover
Feb 18 '17 at 15:47
add a comment |
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)
Great ! I was digging whole stack for this answer !
– Mc.Lover
Feb 18 '17 at 15:47
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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];
add a comment |
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];
add a comment |
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];
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];
edited Feb 26 '12 at 16:56


Costique
22.7k46976
22.7k46976
answered Feb 26 '12 at 2:03
HSGHSG
934916
934916
add a comment |
add a comment |
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)
Works smooth as silk! Thx.
– Oleg Popov
Sep 11 '15 at 13:50
add a comment |
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)
Works smooth as silk! Thx.
– Oleg Popov
Sep 11 '15 at 13:50
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered May 1 '17 at 8:04


Zahidur Rahman FaisalZahidur Rahman Faisal
27547
27547
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Feb 23 '16 at 16:43
giLisHgiLisH
629
629
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Jan 3 '18 at 9:55


Sazzad Hissain KhanSazzad Hissain Khan
15.6k767108
15.6k767108
add a comment |
add a comment |
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];
add a comment |
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];
add a comment |
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];
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];
answered Jul 1 '14 at 14:34
Juan ReyesJuan Reyes
39437
39437
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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