UIPageViewController指示器颜色不变

3

我在这里读了很多关于这个问题的帖子,现在最好的解决方案似乎是将其设置如下:

 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)

}

提前感谢

1个回答

13

现在最好的解决方案是将其设置为以下内容:

不,不是这样。您正在添加一个新的UIPageControl,但UIPageViewController已经有一个UIPageControl。您想要的是设置那个UIPageControl的属性。

做法是使用外观代理(appearance proxy)。例如:

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

谢谢你的回答。你能展示一下如何将它应用到现有的pageViewController中吗,就像在我的代码中的ViewDidLoad()方法里?我不知道如何访问现有的UIPageControl。 - theDC
天哪,我把这段代码放在ViewDidLoad()里面,简直像魔法一样起作用了。谢谢! - theDC
applicationDidFinishLaunching会是一个更好的位置。 - matt
1
我有两个pageViewControllers,但我只想自定义其中一个,我认为将其移动到applicationDidFinishLaunching可能会影响它们两个。 - theDC
1
如果您想自定义特定的页面视图控制器,可以使用UIPageControl.appearance(whenContainedInInstancesOf:) - Mendigo dos Bytes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接