UIPage ViewController,我想从非uipage viewcontroller导航另一个第二个viewcontroller

时间:2017-08-02 07:01:58

标签: ios swift model-view-controller uipageviewcontroller

我需要UIpageViewcontroller的代码,它的工作方式类似于Image的MVC。

PageViewController Flow

编辑:

当我点击"其他ViewController"的下一个按钮时,它将调用UIPageViewcontroller的ViewController 2。 确切的流程如上图所示。

1 个答案:

答案 0 :(得分:1)

好的,这是一种方法

首先你需要在故事板中创建一个UIPageViewController,并将类设置为与此代码相同

在新的swift文件中复制并粘贴此

class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
//make sure the storyboard PageviewControllers class is set to this class or it wont work

lazy var VCArray: [UIViewController] = {
    //here is where your viewController pages go. in storyboard create multiple viewControllers and give them a storyboard ID here is where the storyboard IDs will be put in
    return [self.VCInstance(name: "firstVC"),
            self.VCInstance(name: "secondVC"),
            self.VCInstance(name: "thirdVC")]
}()

private func VCInstance(name: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    if let firstVC = VCArray.first {
        setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }


}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = VCArray.index(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return VCArray.last
    }

    guard VCArray.count > previousIndex else {
        return nil
    }
    return VCArray[previousIndex]
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = VCArray.index(of: viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < VCArray.count else {
        return VCArray.first
    }

    guard VCArray.count > nextIndex else {
        return nil
    }
    return VCArray[nextIndex]
}
//this is optional for the page counter at the bottom
public func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return VCArray.count
}
//this is optional for the page counter at the bottom
public func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArray.index(of: firstViewController) else {
        return 0
    }
    return firstViewControllerIndex
}

}

解释 -

上面的代码非常可定制您可以更改底部是否有页面计数器或使用此页面显示多少页面它将多个storyboard viewControllers转换为单页面viewController并且因为页面是单独的故事板viewControllers你可以把任何你想要的东西放在页面上....如果你仍然感到困惑,请查看YouTube上的视频