gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:25:54 25 4
gpt4 key购买 nike

我有一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com