我按照教程将JSon加载到我的应用程序中,之后我使用该数据显示我通过自定义PageViewController获取的每个Item元素的Page
方便地在设置滚动水平并实现方法presentationCount(for:UIPageViewController)
和PresentationIndex(for:UIPageViewController
点所在的背景是黑色。如何更改颜色?如果有一种方法可以通过Storyboard进行,但也可以通过编程方式(只要它与我的情况兼容)就可以了。
当第一次出现单页时(显示segue),黑色条从右侧滑动时出现故障,变为上半部分,背景颜色为黑色,然后全部为黑色
请注意,在我的情况下,要显示的页数是动态的。
我遵循的教程 对于自定义pageviewcontroller:https://grokswift.com/pageviewcontroller/
我尝试使用自定义点(您可以在我的控制器中看到它作为注释,因为它不起作用)http://www.seemuapps.com/page-view-controller-tutorial-with-page-dots
我的控制员:
import UIKit
class TrainingPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
// var pageControl = UIPageControl()
var items : [Item] = []
// var index = 0
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let currentPageViewController = viewController as? SinglePageViewController, let currentItem:Item = currentPageViewController.item {
let currentIndex = items.index(of: currentItem)
return viewControllerAtIndex(index: currentIndex! - 1)
}
return nil
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
if let currentPageViewController = viewController as? SinglePageViewController, let currentItem:Item = currentPageViewController.item {
let currentIndex = items.index(of: currentItem)
return viewControllerAtIndex(index: currentIndex! + 1)
}
return nil
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.dataSource = self
// self.delegate = self
// configurePageControl()
// FETCH data
if let house = StartViewController.house {
self.items = house.items
if let firstVC = self.viewControllerAtIndex(index: 0) {
let viewControllers = [firstVC]
self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
} else {
print("Error loading data")
}
}
func viewControllerAtIndex(index: Int) -> UIViewController? {
if (index < 0 || index > items.count - 1){
return nil
} else {
let itemAtIndex = items[index] as Item
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SinglePageViewController") as! SinglePageViewController
vc.item = itemAtIndex
// self.index = index
print("showing item \(itemAtIndex.name)")
return vc
}
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return self.items.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
// func configurePageControl() {
// // The total number of pages that are available is based on how many available colors we have.
// pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
// self.pageControl.numberOfPages = items.count
// self.pageControl.currentPage = 0
// self.pageControl.tintColor = UIColor.red
// self.pageControl.pageIndicatorTintColor = UIColor.blue
// self.pageControl.currentPageIndicatorTintColor = UIColor.red
// self.view.addSubview(pageControl)
// }
// func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
// let pageContentViewController = pageViewController.viewControllers![0]
// self.pageControl.currentPage = self.index
// }
}