1

I need to add a button horizontally to a Page Control in UIPageViewController. I have added my pageviewcontroller as a subview to a ViewController, by default im getting the page control .Now I wanted to add a button, Can anyone suggest any way to add button ?

Thanks in advance!!

_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                                                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                        options:nil];

    self.view.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                @"Main" bundle:[NSBundle mainBundle]];
    UIViewController *p1 = [storyboard instantiateViewControllerWithIdentifier:@"First"];
    UIViewController *p2 = [storyboard
                            instantiateViewControllerWithIdentifier:@"Second"];
    UIViewController *p3 = [storyboard
                            instantiateViewControllerWithIdentifier:@"Third"];
    UIViewController *p4 = [storyboard
                            instantiateViewControllerWithIdentifier:@"Fourth"];
    _Array = @[p1,p2,p3,p4];
    _pageViewController.dataSource = self;
    _pageViewController.delegate = self;
    [_pageViewController setViewControllers:@[_Array[0]]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:nil];

    [_pageViewController willMoveToParentViewController:self];
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.view sendSubviewToBack:self.pageViewController.view];    
    [_pageViewController didMoveToParentViewController:self];
meowsush
  • 147
  • 2
  • 13

2 Answers2

1

I believe this is what youre looking for. He explains in detail how to add the buttons "on top of " your page view controller view. You can do this after you add the page view controller, you can use the sendSubviewToBack method to ensure that the buttons remain in the foreground while you will be able to swipe between the different view controllers of your page view controller. Have a look here

Community
  • 1
  • 1
HarmVanRisk
  • 218
  • 1
  • 8
0

you can extract the view where page control is kept on, then you can do what ever you want. Below is the code for extracing the pagecontrol view:

NSArray *subviews = self.pageController.view.subviews;

for (int i=0; i<[subviews count]; i++) {
    if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
        self.pageControl = (UIPageControl *)[subviews objectAtIndex:i];
    }
}
UIView *pageControlBaseView = [[UIView alloc] initWithFrame:CGRectMake(0,60,100, 40)];
[pageControlBaseView addSubview:self.pageControl];
self.pageControl.pageIndicatorTintColor = [UIColor redcolor];
self.pageControl.currentPageIndicatorTintColor = [UIColor greencolor];
[self.view addSubview:pageControlBaseView];

please let me know if you need more clarity.

manish
  • 99
  • 11