Access the UIPageControl created by iOS6 UIPageViewController?

后端 未结 8 1168
盖世英雄少女心
盖世英雄少女心 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 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

提交回复
热议问题