Access UIPageControl created by iOS6 UIPageViewController?

I am using a UIPageViewController with the setting Navigation to Horizontal, the transition style set for scrolling (in InterfaceBuilder), and without a spine. This gives me a wonderful integrated UIPageControl. Now I want me to be able to switch the display (because there is a work underneath it).

I tried setting presentationCountForPageViewController and presentationIndexForPageViewController to return 0 when the UIPageControl supposed to be hidden, but these methods do not call when I want.

Suspending for stacktrace, I see that they are being called [UIPageViewController _updatePageControlViaDataSourceIfNecessary] ... I assume that my application will be rejected if I try to use this method.

Should I search through it objects or view them myself, so that I control them, or is there a better way to switch its visibility?

Thanks!

+6
source share
7 answers

I would say hunting through subviews. This code successfully finds the UIPageControl in the subviews hierarchy:

 NSArray *subviews = pageController.view.subviews; UIPageControl *thisControl = nil; for (int i=0; i<[subviews count]; i++) { if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) { thisControl = (UIPageControl *)[subviews objectAtIndex:i]; } } 

I use this to adjust the color of the dots, I think you could do the same with the alpha value or send it back or something like that.

Apple does not provide a direct interface to UIPageControl through the UIPageViewController class, but it does not require illegal method calls to access it ... I don’t understand why this will lead to application rejection.

+13
source

In Swift :

  let subviews: Array = self.pageViewController.view.subviews var pageControl: UIPageControl! = nil for (var i = 0; i < subviews.count; i++) { if (subviews[i] is UIPageControl) { pageControl = subviews[i] as! UIPageControl break } } 
+3
source

I have implemented a category to handle this for me, which saves me from my code and allows me to access the control page via "pageController.pageControl"

Objective-c

 // Header @interface UIPageViewController (PageControl) @property (nonatomic, readonly) UIPageControl *pageControl; @end 

I also used recursion (handled by blocks) in case Apple decides to change the implementation, as a result of which UIPageControl will not be in the first layer of subviews.

 // Implementation #import "UIPageViewController+PageControl.h" @implementation UIPageViewController (PageControl) - (UIPageControl *)pageControl { __block UIPageControl *pageControl = nil; void (^pageControlAssignBlock)(UIPageControl *) = ^void(UIPageControl *blockPageControl) { pageControl = blockPageControl; }; [self recurseForPageControlFromSubViews:self.view.subviews withAssignBlock:pageControlAssignBlock]; return pageControl; } - (void)recurseForPageControlFromSubViews:(NSArray *)subViews withAssignBlock:(void (^)(UIPageControl *))assignBlock { for (UIView *subView in subViews) { if ([subView isKindOfClass:[UIPageControl class]]) { assignBlock((UIPageControl *)subView); break; } else { [self recurseForPageControlFromSubViews:subView.subviews withAssignBlock:assignBlock]; } } } @end 

It may be redundant for your needs, but it works well for my

+2
source

Quick Expansion 3:

 extension UIPageViewController { var pageControl: UIPageControl? { for view in view.subviews { if view is UIPageControl { return view as? UIPageControl } } return nil } } 
+2
source

You can access this for all PageControl objects using appearance (see the UIAppearance protocol ), but to get a specific instance, d must use recursion. Quick code:

 let pageControl = UIPageControl.appearance() 
+1
source

For swift

To get the points in the page control, we can use

 //dots will be an array of the dots views let dots = pageControl.subviews 

To get the current view of a point

 let currentDot = dots[pageControl.currentPage] 

To get a view of other points

 for i in 0..<dots.count { let dot = dots[i] if i == pageControl.currentPage { //dot => current dot } else { //dot => other dot } } 

After we get the point of view, we can change everything we want, for example

 dot.layer.borderColor = .green dot.layer.borderWidth = 1 
0
source

C # extension:

 public static class PageViewControllerExtension{ public static UIPageControl GetPageControl(this UIPageViewController pageViewController){ foreach (var view in pageViewController.View.Subviews){ var subView = view as UIPageControl; if (subView != null){ return subView; } } return null; } } 
0
source

All Articles