ios - 动态改变UIPageControl.appearance的点的背景颜色

标签 ios swift uipagecontrol

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

           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()

        }
    }



}

enter image description here

enter image description here

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

最佳答案

不幸的是,您无法直接访问 UIPageViewController 中内置的页面控件。因此,您必须使用外观代理才能更改其外观(正如您已经在做的那样)。但外观代理仅影响事物的 future 实例;因此,一旦页面控件已经存在,设置外观代理将不会产生任何影响(正如您所发现的)。

因此,最简单的解决方案是放弃 UIPageViewController 中内置的页面控件,而使用您自己的 UIPageControl,您可以直接更改其点。您必须将其与页面 View Controller 协调,但这并不困难。

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

关于ios - 动态改变UIPageControl.appearance的点的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489469/

相关文章:

ios - Facebook 登录按钮在 WKWebView 中不起作用

swift - 私有(private) IBOutlets Swift

ios - UIPageControl pageIndicator 快速将位置更改为左侧或右侧

ios - 使用 NSDate 的核心数据排序描述符,使用 Swift 的 iOS

ios - 在 Xcode 中显示第一个 Storyboard场景的独特问题

ios - 在解析中用用户的好友列表更新 Tableview

ios - 使用 Tabman 分页 View Controller ,可以将默认的 Tab 键顺序更改为 RTL 吗?

ios - 我可以将 UIPageControl 放在 UITableViewController 的表头中吗?

ios - 我可以只对特定的单元格标识符执行 didSelectRowAtIndexPath 吗?

iphone - UITableView 上冲突的手势识别器