UIPageViewController 指示器不改变颜色

UIPageViewController indicators don't change color

我在这里阅读了很多关于这个问题的帖子,看来现在最好的解决办法是设置如下:

 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)

但出于某种原因,这对我不起作用。页面控件背景颜色改变,但指示器保持白色。

我的整个 viewDidLoad() 方法

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)

}

提前致谢

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

不,不是。您正在添加 new UIPageControl,但是 UIPageViewController already 有一个 UIPageControl。你想要的是设置 that UIPageControl.

的属性

这样做的方法是使用 appearance 代理。示例:

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