I have searched a lot before posting without success:
I have UIPageViewController with array of UIViewController and PageCurl as transition.
Every UIViewController consists of UIScrollView and UIWebView.
the scroll height calculated based on webview height after loading the content.
now when I load UIPageViewController and start navigation between UIViewController without scrolling the content every thing works fine. But if I scrolled any page and then navigate to the next page or previous one. it will redisplay the same page and then works again normally.
I have tested it on IOS7 and IOS8:
UIPageViewController viewDidLoad method:
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageController.doubleSided = NO;
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
View Controller webViewDidFinishLoad:
CGSize contentSize = aWebView.scrollView.contentSize;
webView.frame = CGRectMake(0, 312, contentSize.width, contentSize.height);
scrollView.contentSize = CGSizeMake(320.f, contentSize.height+60);
Edit
Here is the complete code for viewControllerBeforeViewController and viewControllerAfterViewController of UIPageViewController
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [(NewsDetailVC *)viewController index];
self.index = index;
NSLog(#"view before %i", index);
if (index <= 0) {
return nil;
}
index--;
currentViewController = [self viewControllerAtIndex:index];
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [(NewsDetailVC *)viewController index];
NSLog(#"view after %i", index);
if (index >= self.newsList.count -1) {
return nil;
}
index++;
currentViewController =[self viewControllerAtIndex:index];
return [self viewControllerAtIndex:index];
}
Edit2
The previous page is an UIViewController contains UITableView when click on a cell will pass the array of Items to UIPageViewController and the selected index.
The purpose of currentViewController because I have controls for font size, and current view background.
The UIPageViewController parent is UIViewController
Here is the code of viewControllerAtIndex:
- (NewsDetailVC *)viewControllerAtIndex:(NSUInteger)index {
NewsDetailVC *childViewController = [[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"NewsDetailVC"];
childViewController.index = index;
childViewController.selectedNews = [self.newsList objectAtIndex:index ];
childViewController.selectedCategory = self.selectedCategory;
return childViewController;
}
I had the same problem with an UIPageViewController.
It seems like the index shown in viewControllerAtIndex is not quite an accurate one.
As somebody else mentioned in the comment above, in these situations you should implement the delegate method pageViewController:willTransitionToViewControllers.
In this method you should do your work:
- (void)pageViewController:(UIPageViewController *)pageViewController
willTransitionToViewControllers:(NSArray *)pendingViewControllers {
if ([pendingViewControllers count] > 0) {
NSUInteger index = [(PageContentViewController *)
[pendingViewControllers objectAtIndex:0] pageIndex];
if (index == [yourArray count] - 1 || index == 0) {
[self loadContent];
} else {
[self doOtherStuff];
}
}
}
Somehow, the * viewControllerAtIndex* is called several times, giving you troubles finding the index, but in this method the index should be accurate.
Hope it helps!
Related
I can't seem to get a UIPageViewController working. With the code below, the PageViewController calls all of its data source methods and even shows the corresponding dots, but the actual content views are not displayed (even though I checked in debug that the view controllers are instantiated correctly). Does anyone have a clue what I'm doing wrong?
EDIT: the view controller is not nil, but all its properties are. Apparently there is something going wrong with the instantiation.
//content view controller containing a text label
#import "TutorialPhoneContentViewController.h"
#interface TutorialRootViewController ()
#end
#implementation TutorialRootViewController {
NSInteger numberOfViewControllers;
}
- (void)viewDidLoad {
[super viewDidLoad];
numberOfViewControllers = 3;
[self setUpViewControllers];
}
- (void)setUpViewControllers {
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
TutorialPhoneContentViewController *ctrl = [self viewControllerAtIndex:0];
[self.pageViewController setViewControllers:#[ctrl] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self.pageViewController willMoveToParentViewController:self];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (TutorialPhoneContentViewController *)viewControllerAtIndex:(NSInteger)index {
//this returns the correct view controller, checked via debug
TutorialPhoneContentViewController *ctrl = [self.storyboard instantiateViewControllerWithIdentifier:#"tutorialContentViewController"];
ctrl.index = index;
ctrl.text = [NSString stringWithFormat:#"View Controller #%ld", index];
return ctrl;
}
#pragma mark - Page View Controller
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSInteger index = [(TutorialPhoneContentViewController *)viewController index];
if (index == 0) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSInteger index = [(TutorialPhoneContentViewController *)viewController index];
if (index == numberOfViewControllers-1) {
return nil;
}
index++;
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
return numberOfViewControllers;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
return 0;
}
I'm trying to add a UIPageViewController to my App using following code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *viewControllers = #[page0,
page1,
page2];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
self.pageController.dataSource = self;
self.pageController.view.frame = self.view.frame;
[self.pageController setViewControllers:#[[viewControllers firstObject]]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self.pageController willMoveToParentViewController:self];
[self addChildViewController:self.pageController];
[self.view addSubview:self.pageController.view];
[self.pageController didMoveToParentViewController:self];
}
Then problem is that when I run the application, using the Simulator or a physical device, I get a empty screen. If I try to swipe the views I can see that the page that I actually added to the viewControllers array is already there but outside of the screen. Making some debugging with SparkInspector, I can see that the view is actually there but with an offset with the same value equal to the screen width.
I also have implemented the DataSource for the UIPageViewController. Here are the implementation of the delegated methods:
- (NSInteger) presentationCountForPageViewController:(UIPageViewController *)pageViewController {
return 3;
}
- (NSInteger) presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
return 0;
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (viewController == nil) {
return [viewControllers firstObject];
}
NSUInteger index = [viewControllers indexOfObject:viewController];
// NSParameterAssert(index != NSNotFound);
index++;
if (index == viewControllers.count) {
return nil;
}
return [viewControllers objectAtIndex:index];
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (viewController == nil) {
return [viewControllers firstObject];
}
NSUInteger index = [viewControllers indexOfObject:viewController];
if (index == 0) {
return nil;
}
index--;
return [viewControllers objectAtIndex:index];
}
I cannot see why the App is offsetting the view controllers, so any advice will be very helpful.
I need a dynamic UIPageViewController, the number of pages depends on the quantity of items of my array.
I have two views for this. One for the first page and another one for the other pages. It will be always more than 2 pages (two items on my array).
I'm trying to test with fixed values. But the paging it's not right. The index is not correct.
1st problem, the presentationCountForPageViewController [_vc count] returns nil;
2nd problem, the pagination it's not right. I get to the last page, and the first view it's gone, so if I go back, goes to second view, always...
Please, help!
- (void)viewDidLoad {
[super viewDidLoad];
FirstViewController *agenda = (FirstViewController*)[self viewControllerAtIndex:0];
[_vc addObject:#"FirstViewController"];
[_vc addObject:#"SecondViewController"];
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
NSArray *viewControllers = [NSArray arrayWithObject:agenda];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
UIPageControl *pageControlAppearance = [UIPageControl appearanceWhenContainedIn:[UIPageViewController class], nil];
pageControlAppearance.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControlAppearance.currentPageIndicatorTintColor = [UIColor darkGrayColor];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [_vc indexOfObject:viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
//notice here I call my instantiation method again essentially duplicating work I have already done!
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [_vc indexOfObject:viewController];
if (index == NSNotFound) {
return nil;
}
index++;
return [self viewControllerAtIndex:index];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (index == 0){
FirstViewController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
return firstController;
}
else{
SecondViewController *secondController = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
return secondController;
}
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [_vc count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
As per comments/chat, a few things needed resolving:
You need to instantiate the _vc array
You need to add UIViewControllerobjects to this array, not the strings you currently use
You need to amend the viewControllerBeforeViewController and viewControllerAfterViewController methods to return the relevant element from the _vc array, remembering to test that the index remains within the array's bounds, to prevent swiping beyond the final page, or before the first (agenda) page.
I need to perform the transitions of the uipageviewcontroller from right to left. So I look at this and this examples. But difference is I have one viewcontroller to show at a time and pageViewController:spineLocationForInterfaceOrientation: can only return UIPageViewControllerSpineLocationMin not UIPageViewControllerSpineLocationMax. This is what I have done so far.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [(APPChildViewController *)viewController index];
if (index == 0) {
return nil;
}
// Decrease the index by 1 to return
index--;
return [self viewControllerAtIndex:index];}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [(APPChildViewController *)viewController index];
index++;
if (index == 15) {
return nil;
}
return [self viewControllerAtIndex:index];}
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
UIViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
self.pageController.doubleSided = NO;
//Return the spine location
return UIPageViewControllerSpineLocationMin;
}
How can I get the curl working properly with the UIPageViewControllerSpineLocationMin?. Also I cannot use Page-Based Application template.
Based on this I found the solution. This is the code.
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
UIViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
self.pageController.doubleSided = NO;
return UIPageViewControllerSpineLocationMax;
}
I have a series of VCs in a PageViewController that a user navigates left to right through with their fingers. I need to add in buttons that essentially perform the same action as the finger swiping, moving left or right by one through the VCs . How can I do this? Right now I am using these two methods to dynamically setting the VCs as the user swipes:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
Is there someway I can do the same thing if a user clicks a button?
You can programmatically set the currently displayed view controller with a transition animation using setViewControllers:direction:animated:completion: on your page view controller.
Here is an example that presents view controllers with random background colors. You can adjust this to use your specific view controllers.
- (void)viewDidLoad
{
[super viewDidLoad];
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pvc.view.frame = CGRectInset(self.view.bounds, 200, 200);
[self.view addSubview:self.pvc.view];
[self.pvc setViewControllers:#[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
-(UIViewController*)randomVC
{
UIViewController *vc = [[UIViewController alloc] init];
UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
vc.view.backgroundColor = color;
return vc;
}
- (IBAction)previousButtonPressed:(id)sender {
[self.pvc setViewControllers:#[[self randomVC]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
- (IBAction)nextButtonPressed:(id)sender {
[self.pvc setViewControllers:#[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
In case Pages can be navigated by added button (Previous, Next) & also by swipe with page control ON.
Please go through below:
//Set Delegate & Data Source for PageView controller [Say in View Did Load]
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
// PageBefore & After When User Scroll to move next or previous page
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
[_nextBtn setTitle:#"Next" forState:UIControlStateNormal];
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound)
{
return nil;
}
if (index > 0)
{
index--;
}
else
{
return nil;
}
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
if (index < 3)
{
index++;
}else
{
return nil;
}
return [self viewControllerAtIndex:index];}
//To Match Exact Index page view when scrolled & navigated using button action. Place button index when page is been translated.
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{
buttonIndex = (int)((PageContentViewController*) pendingViewControllers.firstObject).pageIndex;}
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
if (buttonIndex == 0)
{
_backButton.hidden = true;
}else if (buttonIndex == [self.pageImages count] - 1)
{
_backButton.hidden = false;
[_nextBtn setTitle:#"Begin" forState:UIControlStateNormal];
}else
{
_backButton.hidden = false;
}
}
//Previous , Next Button Actions
-(void)backBtnClicked:(id)sender{
if (buttonIndex > 0)
{
buttonIndex -= 1;
}
if (buttonIndex < 1) {
_backButton.hidden = YES;
}
if (buttonIndex >=0) {
[_nextBtn setTitle:#"Next" forState:UIControlStateNormal];
PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}}
-(void)nextBtnAction:(id)sender{
if (buttonIndex < 3)
{
buttonIndex += 1;
}
if(buttonIndex == _pageImages.count){
//Navigate Outside Pageview controller
} else{
if (buttonIndex ==3) {
[_nextBtn setTitle:#"Begin" forState:UIControlStateNormal];
}
_backButton.hidden = NO;
PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}}
//BUTTON INDEX
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
return [self.pageImages count];}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
return buttonIndex;}