更改 UIPageViewController 中显示的初始视图控制器
Posted
技术标签:
【中文标题】更改 UIPageViewController 中显示的初始视图控制器【英文标题】:Changing Initial View Controller Shown in UIPageViewController 【发布时间】:2018-01-02 06:01:32 【问题描述】:几个月前我刚刚开始使用 swift 进行编程,目前我正在做一些随机项目,以帮助我更好地学习这门语言。目前,我正在尝试使用 UIPageViewController 创建类似于 Snapchat 的 UI。水平滚动工作正常,我可以正确浏览页面,但我想更改显示的初始视图控制器。在下面的代码中,我有三个 ViewController(vc1、vc2、vc3)。我想让它们保持相同的顺序,但首先显示 vc2,所以基本上把它放在另外两个的中间。感谢所有帮助。谢谢!
import UIKit
import FirebaseDatabase
import FirebaseAuth
import Firebase
class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource
let Cards = CardsVC()
lazy var viewControllerList:[UIViewController] =
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "MessagesVC")
let vc2 = sb.instantiateViewController(withIdentifier: "CardsVC")
let vc3 = sb.instantiateViewController(withIdentifier: "ProfileVC")
return[vc1, vc2, vc3]
()
override func viewDidLoad()
super.viewDidLoad()
//if Auth.auth().currentUser?.uid == nil
//logout()
//else
self.dataSource = self
if let firstViewController = viewControllerList.first
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
//
func logout()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let verifyPhoneVC = storyboard.instantiateViewController(withIdentifier: "enterPhoneNumber")
present(verifyPhoneVC, animated: true, completion: nil)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let vcIndex = viewControllerList.index(of: viewController) else return nil
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else return nil
guard viewControllerList.count > previousIndex else return nil
return viewControllerList[previousIndex]
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
guard let vcIndex = viewControllerList.index(of: viewController) else return nil
let nextIndex = vcIndex + 1
guard viewControllerList.count != nextIndex else return nil
guard viewControllerList.count > nextIndex else return nil
return viewControllerList[nextIndex]
【问题讨论】:
【参考方案1】:你已经准备好了整个代码,你需要做的就是从 viewDidLoad 中的 viewControllerList 传递 secondViewController 就可以了
所以替换
if let firstViewController = viewControllerList.first
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
与
let secondViewController = viewControllerList[1]
self.setViewControllers([secondViewController], direction: .forward, animated: true, completion: nil)
希望对你有帮助
【讨论】:
完美!谢谢! @a-albert: 永远欢迎你,伙计 :) 很高兴我能帮上忙 :) 编码愉快【参考方案2】:在这里,我在 swift 3 中编写了一个扩展,用于在 UIPageViewController 中导航下一页和上一页。您可以根据需要进行修改。
扩展 UIPageViewController
func goToNextPage(animated: Bool = true)
guard let currentViewController = self.viewControllers?.first else return
guard let nextViewController = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) else return
setViewControllers([nextViewController], direction: .forward, animated: animated, completion: nil)
func goToPreviousPage(animated: Bool = true)
guard let currentViewController = self.viewControllers?.first else return
guard let previousViewController = dataSource?.pageViewController(self, viewControllerBefore: currentViewController) else return
setViewControllers([previousViewController], direction: .reverse, animated: animated, completion: nil)
我希望你觉得它有用。 接受建议。
【讨论】:
以上是关于更改 UIPageViewController 中显示的初始视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
更改 UIPageViewController 中显示的初始视图控制器
如何从属于 UIPageViewController 的 UIViewController 之一更改 UIPageViewController?
有没有办法在保持在同一页面上的同时从 UIPageViewController 中更改标签文本?
UIPageViewController 更改导航栏后退按钮标题