Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide dots from UIPageViewController

I would like to do to a pretty simple thing. Just remove all the dots, and the bar on the bottom of the UIPageViewController.

This is the setup: I have a custom view controller which has UIPageViewController *pageController I display it like this:

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

self.pageController.dataSource = self;
[[self.pageController view] setFrame:[self.view bounds]];

BSItemPageViewController *initialViewController = [self viewControllerAtIndex:selectedIndex];

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

Any ideas on how do I remove the dots?

like image 759
Stefan Salatic Avatar asked Dec 23 '13 18:12

Stefan Salatic


4 Answers

The page control is only displayed if the datasource implements these methods:

presentationCountForPageViewController:
presentationIndexForPageViewController:

Simply remove your implementation of these, and the page control will not be displayed. From the datasource docs:

If both of the methods in “Supporting a Page Indicator” are implemented and the page view controller’s transition style is UIPageViewControllerTransitionStyleScroll, a page indicator is visible.

like image 124
jrturton Avatar answered Oct 29 '22 05:10

jrturton


In my situation I have multiple UIPageViewControllers (created from -[UITableView didSelectRowAtIndexPath]), some of which contain only 1 page. Instead of using different controllers for different UITableView rows, I implemented the UIPageViewController delegate method as follows:

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return ([self numberOfPages] == 1 ? 0 : [self numberOfPages]);
}

This returns 0 if there is only 1 page which seems to make UIPageViewController not show the dots. It's a kludge but it appears to work (iOS SDK 7.0).

I suppose a "cleaner" way would be to remove the methods at runtime for those UIPageControllers having only 1 page, but this would involve some fancy objC runtime manipulation.

Comments on this approach?

like image 29
mts Avatar answered Oct 29 '22 04:10

mts


If you want to hide those dots dynamically at runtime, then return -1 from the presentationIndexForPageViewController delegate:

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return yourPageVCs.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return yourPageVCs.count > 1 ? 0 : -1
}

In this case if you have only 1 (single sided) page, dots will be hidden.

like image 4
iOSergey Avatar answered Oct 29 '22 05:10

iOSergey


The above fixes works fine with fixed number of pages at the beginning.

I tried different approaches to solve the problem when pages can be increased or decreased dynamically.

So I used below method which will manually hide the component itself.

func togglePageControl(pageCount: Int, threshold: Int = 1) {

    var hidden = true

    if pageCount > threshold {

        hidden = false

    }

    for subView in self.view.subviews {
        if subView is UIScrollView {
            subView.frame = self.view.bounds
        } else if subView is UIPageControl {
            subView.isHidden = hidden
        }
    }
}

And this should be called from

public func presentationCount(for pageViewController: UIPageViewController) -> Int {

    togglePageControl(pageCount: pages.count)

    // or togglePageControl(pageCount: pages.count, threshold: 5)

    return pages.count
}
like image 2
muhammed basil Avatar answered Oct 29 '22 04:10

muhammed basil