У меня есть UIPagevViewController
, который используется, чтобы показать 4 ViewControllers
.Я хочу, чтобы цвет фона точек менялся в зависимости от цвета фона представления, которым я сейчас занят.Хотя мне удалось получить одинаковый цвет фона для точек и первой страницы, я не смог сделать это для всех остальных.Другими словами, я хочу динамически изменить фон точек на основе отображаемого вида
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](https://i.stack.imgur.com/8DHyh.png)
![enter image description here](https://i.stack.imgur.com/CCCb1.png)
Первое изображение-> Первая страница.Второе изображение-> вторая страница.Посмотрите на цвет фона точек