动态改变UIPageControl.appearance个圆点的背景颜色

Dynamically change the background color of the dots of UIPageControl.appearance

我有一个UIPagevViewController用来显示4ViewControllers。我希望点背景颜色根据我当前进入的视图背景颜色而改变。虽然我已经设法为点和第一页获得相同的背景颜色,但我未能为所有其他页面做到这一点。换句话说,我想根据显示的视图动态更改点背景

           import UIKit

            class PageViewController: UIPageViewController,UIPageViewControllerDelegate, UIPageViewControllerDataSource {

            //my 4 viewControllers used in the UIPageViewController
            fileprivate lazy var pages : [UIViewController] = {
                return [
                    self.getViewController(withIdentifier: "firstViewControllerID"),
                    self.getViewController(withIdentifier: "secondViewControllerID"),
                    self.getViewController(withIdentifier: "thirdViewControllerID"),
                    self.getViewController(withIdentifier: "differentViewController")

                ]
            }()

         //function that accesses those view controllers used above from the storyboard
            fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
                {
                    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
                }
         /* needed function for the UIPageViewControllerDelegate, UIPageViewControllerDataSource protocols 
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
                  //Some Code. Unnecessary for the question
                }

            func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
                 {
                       //Some Code. Unnecessary for the question
                 }
          */
        //function where the dots colours are set
        private func setupPageControl() {
        let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
        appearance.pageIndicatorTintColor = UIColor.gray
        appearance.currentPageIndicatorTintColor = UIColor.red
        appearance.backgroundColor = pages[appearance.currentPage].view.backgroundColor
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        setupPageControl()
        return pages.count
    }


    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }




    override func viewDidLoad() {
        super.viewDidLoad()


       self.dataSource = self
       delegate = self
        if let firstVC = pages.first {
            setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
            setupPageControl()

        }
    }



}

第一张图片->第一页。第二张图片->第二页。看点背景的颜色

遗憾的是,您无法直接访问 UIPageViewController 中内置的页面控件。因此,您必须使用外观代理来更改其外观(正如您已经在做的那样)。但是外观代理只影响事物的未来个实例;因此,一旦页面控件已经存在,设置外观代理将不会有任何影响(如您所见)。

所以最简单的解决办法就是放弃UIPageViewController内置的页面控件,直接使用自己的UIPageControl,可以直接改变点。您必须将其与页面视图控制器协调,但这并不困难。

另一种可能是尝试直接访问 UIPageViewController 的 UIPageControl。这很脆弱,因为没有官方访问权限,但该技术已在此处讨论过,例如 this answer.