RTL languages uipageviewcontroller curl animation for one view controller - ios

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;
}

Related

UIPageViewController does not show its content view controllers

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;
}

UIPageViewController sets view controller out of the screen

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.

UIPageViewController with multiple views

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.

UIPageViewController transition PageCurl with UIScrollView wrong page index

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!

How to properly setup a UIPageViewController

I have added a UIPageViewController to my project for creating a walkthrough screen, but I can seem to understand how to make this to work using storyboards.
First I have defined the two data source methods: viewControllerBeforeViewController and viewControllerAfterViewController.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [(WalktroughScreenPageViewController *)viewController index];
if (index == 0) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [(WalktroughScreenPageViewController *)viewController index];
index++;
if (index == 3) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (WalktroughScreenPageViewController *)viewControllerAtIndex:(NSUInteger)index {
WalktroughScreenPageViewController *childViewController = [[WalktroughScreenPageViewController alloc] init];
childViewController.index = index;
return childViewController;
}
Also for showing the dots at the bottom I added this two methods:
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return 3;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
What I can understand it's how to do the setup in ViewDidLoad. I tried something like this which gives me a EXC_BAD_ACCESS error:
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = self;
WalktroughScreenPageViewController *ic = [self viewControllerAtIndex:0];
[self setViewControllers:#[ic] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
Can someone point me to the right direction ?

Resources