Changing UIPageViewController own PageController regarding Color of Dots

心已入冬 提交于 2019-12-02 22:36:46

You can use UIAppearance to configure UIPageControl colors. This applies to UIPageControls in UIPageViewControllers too.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  UIPageControl *pageControl = [UIPageControl appearance];
  pageControl.pageIndicatorTintColor = [UIColor whiteColor];
  pageControl.currentPageIndicatorTintColor = [UIColor redColor];
}

If you would like to change the UIPageControl's colors for a specific UIPageViewController, you can use the following:

In Swift 3

let pageControl: UIPageControl = UIPageControl.appearance(whenContainedInInstancesOf: [MyPageViewController.self])
pageControl.pageIndicatorTintColor = UIColor.green
// ... any other changes to pageControl

UIPageControl conforms to UIAppearance protocol. The Apple Developper API Reference states about UIAppearance:

Use the UIAppearance protocol to get the appearance proxy for a class. You can customize the appearance of instances of a class by sending appearance modification messages to the class’s appearance proxy.


Therefore, using Swift 2.2, you may set UIPageControl's pageIndicatorTintColor and currentPageIndicatorTintColor in a subclass of UINavigationController or in your AppDelegate class (for a more global approach).

CustomNavigationController.swift:

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set pageIndicatorTintColor and currentPageIndicatorTintColor
        // only for the following stack of UIViewControllers
        let pageControl = UIPageControl.appearance()
        pageControl.pageIndicatorTintColor = UIColor.blueColor()
        pageControl.currentPageIndicatorTintColor = UIColor.greenColor()
    }

}

AppDelegate.Swift:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Set pageIndicatorTintColor and currentPageIndicatorTintColor globally
        let pageControl = UIPageControl.appearance()
        pageControl.pageIndicatorTintColor = UIColor.blueColor()
        pageControl.currentPageIndicatorTintColor = UIColor.greenColor()

        return true
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!