11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

navigationControllerの左スワイプで戻る機能を無効化する方法

Posted at

iOS7移行、デフォルトで「左スワイプしたら前の画面に戻る」ようになってしまった。うざい。
特定画面で無効化する方法

xcode7
iOS7~9

無効化する方法(iOS7~9で動く…と思う)

全部で動く(と思う)が、めんどくさい方法
例: NavigationControler -(rootViewController)-> HogeViewController

HogeViewController.swift
class HogeViewController: UIViewController, UIGestureRecognizerDelegate {

	// navigationcontrollerに元から設定されているdelagateを保持しておく
	private var originalNavigationControllerDelegate: UIGestureRecognizerDelegate?

	override func viewWilAppear(animated: Bool) {
		super.viewWillAppear(animated)

		// popGestureを乗っ取り、左スワイプでpopを無効化する
		// 必ずdisappearとセットで用いること
		if let popGestureRecognizer = navigationController?.interactivePopGestureRecognizer {
			self.originalNavigationControllerDelegate = popGestureRecognizer.delegate
			popGestureRecognizer.delegate = self
		}
	}

	override func viewWillDisappear(animated: Bool) {
		super.viewWillDisappear(animated)

		// popGestureを乗っ取り、左スワイプでpopを無効化する(のを解除する)
		// 必ずwillAppear/willDisappearとセットで用いること
		if let popGestureRecognizer = navigationController?.interactivePopGestureRecognizer {
			popGestureRecognizer.delegate = originalNavigationControllerDelegate
			originalNavigationControllerDelegate = nil
		}
	}

	// MARK: - UIGestureRecognizerDelegate

    // popGestureを乗っ取り、左スワイプでpopを無効化する
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }
}

簡単に無効化する方法(iOS8.0~iOS8.4では動かない)

iOS8系で動かないけど簡単な方法
http://blog.nambo.jp/2013/11/29/disable-uinavigationcontroller-popgesture/

iOS8で動かない(実際実機で動かなかったが)情報
http://stackoverflow.com/questions/17209468/how-to-disable-back-swipe-gesture-in-uinavigationcontroller-on-ios-7

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?