DCDC DCDC - 5 months ago 26
Swift Question

UIPageViewController indicators don't change color

I've read many other threads here about this problem, it appears that the best solution now is to set it as follows:

let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor(red: 0, green: 147/255, blue: 229/255, alpha: 1)
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
view.addSubview(pageControl)


But for some reason, this does not work for me. Page control background changes its color, but the indicators remain white.

My entire viewDidLoad() method

override func viewDidLoad() {
super.viewDidLoad()
self.pageViewController = self.storyboard!.instantiateViewControllerWithIdentifier("OnboardingPageViewController") as! UIPageViewController

self.pageViewController.dataSource = self
let startingViewController:OnboardingPageContentViewController = self.viewControllerAtIndex(0)!

let viewControllers:Array<OnboardingPageContentViewController> = [startingViewController]

self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor(red: 0, green: 147/255, blue: 229/255, alpha: 1)
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
view.addSubview(pageControl)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)

}


Thanks in advance

Answer

it appears that the best solution now is to set it as follows:

No, it isn't. You are adding a new UIPageControl, but the UIPageViewController already has a UIPageControl. What you want is to set the attributes of that UIPageControl.

The way to do that is to use the appearance proxy. Example:

    let proxy = UIPageControl.appearance()
    proxy.pageIndicatorTintColor = UIColor.redColor().colorWithAlphaComponent(0.6)
    proxy.currentPageIndicatorTintColor = UIColor.redColor()
    proxy.backgroundColor = UIColor.yellowColor()
Comments