Access the UIPageControl created by iOS6 UIPageViewController?

后端 未结 8 1038
盖世英雄少女心
盖世英雄少女心 2021-01-05 02:42

I\'m using a UIPageViewController with Navigation set to Horizontal, Transition Style set to Scroll (in InterfaceBuilder), and no spine. Which gives me a lovely

相关标签:
8条回答
  • 2021-01-05 02:47

    For Swift

    To get the dots in the page control we can use

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

    To get the current dot view

    let currentDot = dots[pageControl.currentPage]
    

    To get the other dots views

    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 dot view we can change whatever we want like

    dot.layer.borderColor = .green
    dot.layer.borderWidth = 1
    
    0 讨论(0)
  • 2021-01-05 02:48

    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
            }
        }
    
    0 讨论(0)
  • 2021-01-05 02:50

    I would say, hunt through the 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'm using this to customize the color of the dots, I imagine you could do the same with the alpha value or send it to the back or something.

    Apple provides no direct interface to the UIPageControl through the UIPageViewController class, but there are no illegal method calls required in order to get to it... I don't see why this would result in an app rejection.

    0 讨论(0)
  • 2021-01-05 03:00

    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 讨论(0)
  • 2021-01-05 03:09

    I implemented a category to handle this for me which gets the mess out of my code and allows me to access the pageControl 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 causing the UIPageControl to 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
    

    This may be overkill for your needs but it worked well for mine

    0 讨论(0)
  • 2021-01-05 03:11

    Swift 3 Extension:

    extension UIPageViewController {
        var pageControl: UIPageControl? {
            for view in view.subviews {
                if view is UIPageControl {
                    return view as? UIPageControl
                }
            }
        return nil
        }
    }
    
    0 讨论(0)
提交回复
热议问题