更改UIPageViewController中显示的初始视图控制器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改UIPageViewController中显示的初始视图控制器相关的知识,希望对你有一定的参考价值。

几个月前我刚开始用swift编程,我目前正在做一些随机项目来帮助我更好地学习语言。目前,我正在尝试使用UIPageViewController创建类似于Snapchat的UI。水平滚动工作正常,我可以正确浏览页面,但我想更改显示的初始视图控制器。在下面的代码中,我有三个ViewControllers(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]
    }

}
答案

您已准备好整个代码,您需要做的就是从viewDidLoad中的viewController列表中传递第二个ViewController,这是全部

所以更换

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)

希望这可以帮助

另一答案

在这里,我在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 页面控件背景颜色

如何在 UIPageViewController 中更改页面指示器的背景颜色

UIPageViewController 中导航栏的更改无法正常工作

如何从属于 UIPageViewController 的 UIViewController 之一更改 UIPageViewController?

有没有办法在保持在同一页面上的同时从 UIPageViewController 中更改标签文本?