我已经设置了我的pageViewController(嵌入在NavigationController中),其中包含一个ViewControllers数组,用作页面(如下所示):
`
class PageViewController: UIPageViewController {
var arrayIndex: Int = 0
var navigationBar = UINavigationBar()
var pageControl = UIPageControl.self
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
// set up & order all view controllers
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newDayViewController("monday"),
self.newDayViewController("tuesday"),
self.newDayViewController("wednesday"),
self.newDayViewController("thursday")//,
self.newDayViewController("friday"),
self.newDayViewController("saturday"),
self.newDayViewController("sunday")
]
}()
// instantiate view controllers above that contain 'day' (easiest method to differentiate from other controllers)
private func newDayViewController(day: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(day)")
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (!completed) {
return
}
arrayIndex = Int(pageViewController.viewControllers!.first!.view.restorationIdentifier!)!
switch arrayIndex {
case 0:
navigationBar.topItem!.title = "Monday"
break
case 1:
navigationBar.topItem!.title = "Tuesday"
break
case 2:
navigationBar.topItem!.title = "Wednesday"
break
case 3:
navigationBar.topItem!.title = "Thursday"
default:
navigationBar.topItem!.title = "Timetable"
}
}
}
extension PageViewController: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
//MARK: - determine which viewController the user is on
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
`
我想知道我是如何拥有它的,以便当用户轻扫页面视图时,UINavigationBar的标题会更改为他们正在查看的视图的标题?例如:当用户查看mondayViewController时,标题是星期一,他们可以刷到星期二,标题也会改变等。
欢迎任何建议
谢谢!
答案 0 :(得分:1)
您的代码中存在一些错误,首先不存在UIPageViewControllerDelegate
,因此永远不会调用委托方法。
然后,我不知道你的PageViewController
是否真的有UINavigationController
。通过选择PageViewController
确保您可以嵌入它,并在图片中做同样的事情:
所以你可以在你的故事板中看到这样的UINavigationController
和UIPageViewController
:
在您为每个故事板ID创建了一天UIViewController
之后,例如星期一:
您可以修改代码,如下面的代码:
import UIKit
class PageViewController: UIPageViewController,UIPageViewControllerDelegate {
var arrayIndex: Int = 0
var pageControl = UIPageControl.self
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newDayViewController("monday"),
self.newDayViewController("tuesday"),
self.newDayViewController("wednesday"),
self.newDayViewController("thursday"),
self.newDayViewController("friday"),
self.newDayViewController("saturday"),
self.newDayViewController("sunday")
]
}()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
if let firstViewController = orderedViewControllers.first {
self.navigationController!.navigationBar.topItem!.title = "monday"
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
private func newDayViewController(day: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(day)")
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if (!completed) {
return
}
if let firstViewController = viewControllers?.first,
let arrayIndex = orderedViewControllers.indexOf(firstViewController) {
switch arrayIndex {
case 0:
self.navigationController!.navigationBar.topItem!.title = "Monday"
break
case 1:
self.navigationController!.navigationBar.topItem!.title = "Tuesday"
break
case 2:
self.navigationController!.navigationBar.topItem!.title = "Wednesday"
break
case 3:
self.navigationController!.navigationBar.topItem!.title = "Thursday"
default:
self.navigationController!.navigationBar.topItem!.title = "Timetable"
}
}
}
}
extension PageViewController: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
//MARK: - determine which viewController the user is o
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}