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:24

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.

查看更多
Evening l夕情丶
3楼-- · 2019-02-17 02:27

How about a nice, up to date Swift 1-liner?

let pageControl = view.subviews.first { $0 is UIPageControl } as? UIPageControl

Or if you like an extension:

extension UIPageViewController {
    var pageControl: UIPageControl? {
        return view.subviews.first { $0 is UIPageControl } as? UIPageControl
    }
}
查看更多
Root(大扎)
4楼-- · 2019-02-17 02:28

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
        }
    }
查看更多
地球回转人心会变
5楼-- · 2019-02-17 02:32

Swift 3 Extension:

extension UIPageViewController {
    var pageControl: UIPageControl? {
        for view in view.subviews {
            if view is UIPageControl {
                return view as? UIPageControl
            }
        }
    return nil
    }
}
查看更多
乱世女痞
6楼-- · 2019-02-17 02:32

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
查看更多
孤傲高冷的网名
7楼-- · 2019-02-17 02:37

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;
    }
}
查看更多
登录 后发表回答