Changing UIPageViewController's page programmatically doesn't update the UIPageControl
to update your UIPageControl indicator, you need to implement one data source method of UIPageViewController (the UIPageViewControllerDataSource method) :
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
This method is responsible for updating the page control indicator (when you use UIPageViewController). You only need to return the currentpage value in this method. The Method gets called by default when you use/make a call for setViewControllers on your custom UIPageViewController.
So the chunk of code that you need to write is:
- (void)scrollToNext
{
UIViewController *current = self.viewControllers[0];
NSInteger currentIndex = [self.model indexForViewController:current];
UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
if (nextController) {
NSArray *viewControllers = @[nextController];
// This changes the View Controller and calls the presentationIndexForPageViewController datasource method
[self setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return currentIndex;
}
Hope this solves your problem. :)
Is it possible to Turn page programmatically in UIPageViewController?
Yes it is possible with the method:
- (void)setViewControllers:(NSArray *)viewControllers
direction:(UIPageViewControllerNavigationDirection)direction
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion;`
That is the same method used for setting up the first view controller on the page. Similar, you can use it to go to other pages.
Wonder why viewControllers
is an array, and not a single view controller?
That's because a page view controller could have a "spine" (like in iBooks), displaying 2 pages of content at a time. If you display 1 page of content at a time, then just pass in a 1-element array.
An example in Swift:
pageContainer.setViewControllers([displayThisViewController], direction: .Forward, animated: true, completion: nil)
Programmatically-added page control disappears when setting view controllers after setup
Create a single UIPageControl
that you put in the WalkthroughRootViewController and update it when you navigate the pages - don't create a page control for each child.
Try not to use extension
s to override methods - it can cause you trouble - see this blog entry.
Reloading Views in UIPageViewController
After a long time and after wasting a support ticket with Apple, I decided to go with another approach.
In my Storyboard I created 2 different navigation controllers, one for each case (if there are somethings
and if there aren't any), and I have an initial view Controller that, on viewDidAppear
, instantiates one of the two and presents it. This way, I make sure that the entire navigation controller gets reloaded correctly.
UINavigationController *nc;
if ([Settings somethings].count)
{
nc = [self.storyboard instantiateViewControllerWithIdentifier:@"NavigationController"];
}
else
{
nc = [self.storyboard instantiateViewControllerWithIdentifier:@"NoSomethingNavigationController"];
}
self.nc = nc;
[self presentViewController:nc animated:NO completion:nil];
How to change position of UIPageControl in iOS =8.0?
Yes, you can add custom page controller for that.
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)]; // your position
[self.view addSubview: self.pageControl];
then remove
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
and
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
Then add another delegate method:
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
{
PageContentViewController *pageContentView = (PageContentViewController*) pendingViewControllers[0];
self.pageControl.currentPage = pageContentView.pageIndex;
}
UIPageViewController, how do I correctly jump to a specific page without messing up the order specified by the data source?
Programming iOS6, by Matt Neuburg documents this exact problem, and I actually found that his solution feels a little better than the currently accepted answer. That solution, which works great, has a negative side effect of animating to the image before/after, and then jarringly replacing that page with the desired page. I felt like that was a weird user experience, and Matt's solution takes care of that.
__weak UIPageViewController* pvcw = pvc;
[pvc setViewControllers:@[page]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES completion:^(BOOL finished) {
UIPageViewController* pvcs = pvcw;
if (!pvcs) return;
dispatch_async(dispatch_get_main_queue(), ^{
[pvcs setViewControllers:@[page]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO completion:nil];
});
}];
Related Topics
Multiple Uitableview in Single Viewcontroller
Ios- Check Whether Current Time Is Between Two Times or Not
Supportedinterfaceorientations Not Working
Detecting When App Is Becoming Active from Lockscreen VS Other on iOS7
Uisegmentedcontrol iOS 13 Clear Color
What Does $0 Represent in Closures in Swift
Parsing JSON Using the New Swift 3 and Alamofire
Why Is -Force_Load No Longer Required for My Three20 Dependencies in Xcode 4.2
Xamarin.iOS Binding Libraries/Native Frameworks
iOS Format String into Minutes and Seconds
Get Index or Tag Value from Imageview Tap Gesture
How to Get Current Location Using Cllocationmanager in iOS
How to Unit Test an App Extension on Xcode 6
How to Add Firebase to Today Extension iOS
How to Show Specific Language Keyboard When User Input Values in Uitextfield in iPhone App
Undefined Symbols for Architecture Armv7 for Cocoapods Libraries
Initializing Swift Properties That Require "Self" as an Argument