Circles from Page View Controller iOS 7 - ios

I have created a Page View Controller. I can load the content view controller with the array to load the Page View Controller (code below). But in the content view, the controller doesn't load the "circles" to scroll pages. What is the problem?
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CondicionPageViewController"];
self.pageViewController.dataSource = self;
NMCondicionPageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];

You need to set your page view controller's data source and implement the method
presentationCountForPageViewController:. In that method you will return the count for the number of dots you want.
You're also probably going to want to implement
presentationIndexForPageViewController: to return the index for which dot you want highlighted.

Related

Unhiding the navigation bar after popping a UIPageViewController

I am having trouble trying to unhide the navigation bar in my app. I have a UIPageViewController created in a UIViewController like so:
//Hide the bar
self.navigationController.navigationBar.hidden=YES;
// Create page view controller
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:229/255.0 green:145/255.0 blue:217/255.0 alpha:1];
pageControl.backgroundColor = [UIColor whiteColor];
//Call our helper method
StickerContentViewController *startingViewController = [self viewControllerAtIndex:0];
//Need to pass the first one, don't put them all in here
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//setup
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
When I pop the UIViewController (the one containing the UIPageViewController) using [self.navigationController popViewControllerAnimated:YES]; I can't get the navigation bar to unhide for the previous screen. I have tried placing self.navigationController.navigationBarHidden=NO; in the viewWillDisappear, viewDidDissappear. I also tried putting it in the content view controller but still no luck. Could someone give me a pointer to what I might be doing wrong here please?
Fixed it, just posting in case someone else has an issue. I was using self.navigationController.navigationBar.hidden=YES; when I should have used self.navigationController.navigationBarHidden=YES;. Not sure what the difference is. Anyway I hide the bar in the viewDidLoad and unhide it in the viewWillDisappear and it works now. thanks guys

UIPageViewController, instantiating ViewControllers with Storyboard ID, Objective-C

I am trying to implement UIPageViewController and I believe that I am in the last stage. At first the UIPageViewController only used one VC to scroll to a "different" page. Now i want it to scroll between different ViewControllers.
This is the code i am using:
- (void)viewDidLoad {
[super viewDidLoad];
self.vcIdentifiers = #[#"PageContentViewController", #"PageContentViewController1"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
UIViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.vcIdentifiers count] == 0) || (index >= self.vcIdentifiers.count)) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
I belive it is in the viewControllerAtIndex i need to make some changes. I thought of doing this:
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.vcIdentifiers[index]];
But then it still would initialise the PageContentViewController. So my problem is i don't know how to initialise the different ViewControllers.

Add Custom View on UIPageViewController

I want to add a Custom View (red View) on the bottom of my UIPageViewController like this:
Because i want some Standard Text which is displayed on every View of my UIPageViewController. Everythings works fine, but my Custom VIew is not visible, this is what i tried:
- (void)viewDidLoad
{
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
FirstTutorialViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
//Here i try to set the Custom view:
[[self.pageController view] addSubview:[self customView]];
Don't add self customView to the page controller, and be sure to set its frame first.
Instead, resize the page controller to allow space at the bottom for the custom view and add both to your main view.

Reloading Views in UIPageViewController

I am making an iOS application that has a UIPageViewController that is loaded with a default page if there aren't any others added yet, and a user can add pages as he progresses through the app. However, the initial view never changes as others are added.
I've tried the answers from these questions:
Is it possible to Turn page programmatically in UIPageViewController?
Changing UIPageViewController's page programmatically doesn't update the UIPageControl
Removing a view controller from UIPageViewController
Refresh a UIPageViewController
None of them work for me. The view still remains at the initial page until I reload the application, after which, the pages show fine, and I can add as many as I want.
This is the code I have for making the PageView and the pages
- (void)viewDidLoad
{
[super viewDidLoad];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self pageControllerView] bounds]];
UIViewController *initialViewController = [self viewControllerAtIndex:0];
__block Page1ViewController *blocksafeSelf = self;
// This doesn't work either... :(
void (^completionBlock)(BOOL) = ^(BOOL finished)
{
// It seems that whenever the setViewControllers:
// method is called with "animated" set to YES, UIPageViewController precaches
// the view controllers retrieved from it's data source meaning that
// the dismissed controller might not be removed. In such a case we
// have to force it to clear the cache by initiating a non-animated
// transition.
usleep(1);
dispatch_async(dispatch_get_main_queue(), ^(){
[blocksafeSelf.pageController setViewControllers:#[initialViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
});
};
[self.pageController setViewControllers:#[initialViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:completionBlock];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
}
- (void)reloadViews
{
[[self childViewControllers] makeObjectsPerformSelector:#selector(removeFromParentViewController)];
[[self.view subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
[self.pageController willMoveToParentViewController:nil];
[self.pageController removeFromParentViewController];
[self.pageController.view removeFromSuperview];
self.pageController = nil;
self.pageController = [[UIPageViewController alloc] init];
[self.view setNeedsDisplay];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((SomethingViewController *)viewController).index;
index++;
if (index >= [Settings somethings].count)
{
return nil;
}
return [self viewControllerAtIndex:index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard_iPhone"
bundle:nil];
if ([Settings somethings].count == 0 && index == 0)
{
NoSomethingViewController *vc = [sb instantiateViewControllerWithIdentifier:#"NoSomethingViewController"];
vc.index = index;
return vc;
}
SomethingViewController *childViewController = [sb instantiateViewControllerWithIdentifier:#"SomethingViewController"];
childViewController.something = [[Settings somethings] somethingAtIndex:index];
childViewController.index = index;
return childViewController;
}
When a new page should be created, by adding a something in the somethings array, I call reloadViews from the View controller that did the adding of the something. The code then hits the second part of viewControllerAtIndex, but that is not shown on the page that I see on the phone, as I still see the NoSomethingViewController and it's view.
If you need more code, I'll provide.
add the following code in the pageViewController viewWillAppear method
dataSource = self
and in the viewWillDisappear add
dataSource = nil
this will force the pageViewController to reload its dataSource
To show the current dot, dont forget you have to update the page indicator
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
Example how to update your UIPageControl indicator https://stackoverflow.com/a/22576250/3897011
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];
In order to reload UIPageViewController you can use the following code:
[self.pageController setViewControllers:#[yourCurrentController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
Also please implement
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
method.

top bar in UIPageViewController

I based my application with the default "Page-based App" in Xcode. I'm trying to display a navigation bar on top of my DataViewController in my storyboard without using a navigation controller. The problem is, when I swipe left and right, the navigation bar seems to go along with the gesture. How can I make it "not-movable"?
Things I tried:
Putting the navigation bar in my RootViewController instead. When I simulate the app, the navigation bar is not visible.
EDIT:
RootViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
GJDataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0);
}
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
DataViewController.m
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.dataLabel.text = [self.dataObject description];
}
Follow these steps:
Use your 2nd trial: "Putting the navigation bar in my RootViewController instead." to make navigation-bar immovable with every age transition.
Now in RootViewController.m, change the line CGRect pageViewRect = self.view.bounds; to CGRect pageViewRect = CGRectMake(self.view.bounds.origin.x, 64, self.view.bounds.size.width, self.view.bounds.size.height-64); .Where 64=20(status-bar height) + 44(navigation-bar) height.
2nd step will adjust frame of your DataViewController's view below added navigation-bar in RootViewController and thus navigation-bar will not hide.
Hope, it helps you....Happy Coding! :-)

Resources