禁用导航UIPageViewController

时间:2015-03-23 16:31:29

标签: ios swift uipageviewcontroller

我有一个带有UIPageViewController实现的应用程序,带有14个ViewControllers,我需要禁用导航直到动画完成,但默认情况下,我可以触摸或滑动以更改视图......

编辑:

PageViewController.swift

import UIKit

class PageViewController: UIViewController, UIPageViewControllerDataSource {
var contentPageRestorationIDs = [String]()
var pageViewController = UIPageViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    contentPageRestorationIDs = ["FirstView", "SecondView", "ThirdView", "FourthView", "FiveView", "SixView", "SevenView", "EightView", "NineView", "TenView", "ElevenView", "TwelveView", "ThirteenView", "FourteenView"]
    // Do any additional setup after loading the view, typically from a nib.

    pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as UIPageViewController
    pageViewController.dataSource = self

    var startingController: UIViewController = self.viewControllerAtIndex(0)!

    pageViewController.setViewControllers([startingController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)


}

func goToPreviousContentViewController(){

    var currentViewController: AnyObject = self.pageViewController.viewControllers[0]
    var vcRestorationID = currentViewController.restorationIdentifier
    var index = find(contentPageRestorationIDs, vcRestorationID!!)

    var previousViewController: UIViewController = self.viewControllerAtIndex(index! - 1)!

    pageViewController.setViewControllers([previousViewController], direction: UIPageViewControllerNavigationDirection.Reverse, animated: false, completion: nil)



}

func goToNextContentViewController(){

    var currentViewController: AnyObject = self.pageViewController.viewControllers[0]
    var vcRestorationID = currentViewController.restorationIdentifier
    var index = find(contentPageRestorationIDs, vcRestorationID!!)

    var nextViewController: BaseContentViewController = self.viewControllerAtIndex(index! - 1)!

    pageViewController.setViewControllers([nextViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> (UIViewController?){
    var vcRestorationID = viewController.restorationIdentifier
    var index = find(contentPageRestorationIDs, vcRestorationID!)

    if(index == 0){
        return nil
    }
    return viewControllerAtIndex(index! - 1)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> (UIViewController?){
    var vcRestorationID = viewController.restorationIdentifier
    var index = find(contentPageRestorationIDs, vcRestorationID!)

    if(index == contentPageRestorationIDs.count - 1){
        return nil
    }
    return viewControllerAtIndex(index! + 1)


}


func presentationCountForPageViewController(pageViewController: UIPageViewController) -> (Int){
    return self.contentPageRestorationIDs.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> (Int){
    return 0
}


func viewControllerAtIndex(index: Int)-> (BaseContentViewController!) {

    if(index >= contentPageRestorationIDs.count){
        return nil
    }

    var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))

    var contentViewController: BaseContentViewController = storyboard.instantiateViewControllerWithIdentifier(self.contentPageRestorationIDs[index]) as BaseContentViewController

    contentViewController.rootViewController = self

    contentViewController.loadView()

    return contentViewController

}
}

BaseContentViewController.swift(我的所有ViewControllers都从此扩展)

import UIKit

class BaseContentViewController: UIViewController {

var rootViewController: PageViewController = PageViewController()
var goNext = Bool()
override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

3 个答案:

答案 0 :(得分:0)

您可以根据自己的要求尝试以下选项

推荐

self.navigationItem.leftBarButtonItem.enabled = NO;

self.navigationItem.rightBarButtonItem.enabled = NO;

或简单地禁用边缘情况

self.view.window.userInteractionEnabled = NO;

更新:最近Apple不允许后退按钮启用/禁用。而不是我们可以隐藏它。

self.navigationItem.hidesBackButton = YES;

答案 1 :(得分:0)

通常,最好不要阻止用户使用UI,也可以终止动画。

显然,这在您的用例中是不可能的。您可以在应用程序中创建标志,指示是否可以显示下一个控制器并在viewControllerAfterViewController中创建逻辑以防止显示下一个控制器。

答案 2 :(得分:0)

我希望我的问题能正确使用

navigationController?.navigationBar.hidden = true

然后在动画结束后再次显示

navigationController?.navigationBar.hidden = false