1

I would like to use UIPageControl to navigate between different view controllers. I have 2 view controllers and another main view controller contains the UIPageControl. The 2 view controllers to be navigated between have been added as children to the main view controller. When I currently start the application, I see a blank screen. I used pageControl.currentPage = 0 but I am unable to understand why I am unable to see the views.

Note that I do not want to use UIScrollView in my application.

Edit - I have followed this tutorial http://www.wannabegeek.com/?p=168

But it uses scroll view. I do not want to use scroll view since it is creating some issues with a UIWebView and a CorePlot which I use in the different view controllers.

I seek guidance as to how to modify this tutorial without using scroll view. Thanks!

Dinesh
  • 2,194
  • 3
  • 30
  • 52

3 Answers3

1

UIPageControl doesn't handle swiping or page switching for you. In order to use it, you NEED to have a scroll view (or some other kind of control) to handle the swipes and the actual page switching, and then implement the UIScrollViewDelegate protocol (or whatever protocol you need for the control you're using) to know when the page control should update it's current page position (the currently selected dot). UIPageControl itself doesn't really do a whole lot.

lottscarson
  • 578
  • 5
  • 14
  • I tried using `UIScrollView` but I have a UIWebView in one of the view controllers and the scroll is not working in the UIWebView. – Dinesh Jan 16 '14 at 20:17
  • 1
    You don't need a scroll view, `UIView` animations to change subview frames will do fine. – Wain Jan 16 '14 at 20:34
1

Adding the other view controllers as children just associates the view controllers - it doesn't add the views they manage as subviews. So, when you add them as children you should also add the subviews and set the initial frames. You might also want to add swipe gestures to trigger view animations to animate the frames of the views (though there are several other options, like using a scroll view).

As part of the animation to move the views you manually update the page control (that won't happen automatically).


The loadScrollViewWithPage method is the key. Call this with parameter 0 and then 1 to load both of the pages and add the views as subviews. Just change self.scrollView to self.view.

I would add a (two really, but get 1 working first) swipe gesture to the page control. Then when it is swiped you can move the other views.

The swipe gesture callback:

- (void)swipeRightToLeftGesture:(UIGestureRecognizer *)gestureRecognizer
{
    [UIView animateWithDuration:1
                     animations:^{

                     for (UIViewController *vc in self.childViewControllers) {
                         CGRect frame = vc.view.frame;
                         frame.origin.x -= self.view.frame.size.width;
                         vc.view.frame = frame;
                     }
                 }];
}

This iterations over the child view controllers and animates the frames of their views to move them to the left. The swipe left to right handler will be the same except it will += the width to move the to the right.

[Code Edited - syntax]

Dinesh
  • 2,194
  • 3
  • 30
  • 52
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Could you please suggest some sample code as to how to add the subviews and set the initial frames? – Dinesh Jan 16 '14 at 20:50
  • 1
    Can you show some code for how you have created what you have? You are obviously struggling to correlate suggestions back to what you have so give details of what you have and we can work in the names you understand. Is this your first app? – Wain Jan 16 '14 at 20:52
  • Can we continue this discussion in a chat room or some other way? I have some basic questions. Could you please answer them. – Dinesh Jan 16 '14 at 21:25
  • But how do I change the dot indicating the page? I used `pageControl.currentPage` but it did not work. – Dinesh Jan 17 '14 at 17:44
  • What did you try setting it to? Did you log the value? Did you get an error? – Wain Jan 17 '14 at 17:47
  • `if (self.pageControl.currentPage == 0) { self.pageControl.currentPage = 1;}` but the page is changed and not the dot. – Dinesh Jan 17 '14 at 17:49
  • I have been working on it. The outlet is set. Yet it does not work. I have posted another question: http://stackoverflow.com/questions/21193217/ios-uipagecontrol-page-indicator-dots-not-changing-with-uiswipegesturerecogniz – Dinesh Jan 22 '14 at 15:52
  • Do we have to load both the view controllers initially? Can we load them on demand. I mean - can we load the second view controller only when the user swipes? – Dinesh Jan 25 '14 at 20:36
  • You can lazy load. Just check when you come to animate and load if required. – Wain Jan 25 '14 at 22:42
  • Hi, I am having some problem when using 3.5 inch and 4 inch only with the pageviewcontroller.. without that, the resizing is being done automatically - is loading pages using page control creating a problem? – Dinesh Feb 21 '14 at 23:03
  • What kind of problem, what resizing? Are you setting the frame before adding subviews? What to? – Wain Feb 21 '14 at 23:12
0

UIPageControl does not do anything much except display some dots. If you want a way of switching between view controllers it sounds like you want UIPageViewController, which is a completely different animal. A nice feature of UIPageViewController is that it can display a UIPageControl as a way of indicating / switching view controllers, which may be just the kind of thing you're after.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • My application currently has 2 views and I would like a page indicator to be incorporated to it. One of the views has a UIWebView and another has a Coreplot which can be zoomed in/out. So do you recommend using UIPageViewController? – Dinesh Jan 16 '14 at 20:05