Access the UIPageControl created by iOS6 UIPageVie

2019-02-17 01:44发布

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 UIPageControl integrated. Now I want to be able to toggle whether it's displaying (because there's artwork underneath it).

I've tried setting presentationCountForPageViewController and presentationIndexForPageViewController to return 0 when the UIPageControl is supposed to be hidden, but those methods aren't being called when I want.

Pausing for stacktrace, I see them being called by [UIPageViewController _updatePageControlViaDataSourceIfNecessary]...I assume my app would be rejected if I tried to use that method.

Should I hunt through subviews for it, or roll my own so I have control over it, or is there some better way to toggle its visibility?

Thanks!

8条回答
迷人小祖宗
2楼-- · 2019-02-17 02:38

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

查看更多
地球回转人心会变
3楼-- · 2019-02-17 02:42

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

let pageControl = UIPageControl.appearance()
查看更多
登录 后发表回答