21
15

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.

スワイプで戻るを簡単に実装

Last updated at Posted at 2018-10-31

#スワイプバック
戻るボタンを押してpopViewControllerをするのってめんどくさいですよね。
TwitterとかSlackとか有名アプリはいちいち一番指の届きにくい左上の戻るボタンをタップしなくても、
スワイプで戻れるように実装されています。

#手順

  1. UIViewControllerのExtensionを書きます
import UIKit

extension UIViewController {

    func setSwipeBack() {
        let target = self.navigationController?.value(forKey: "_cachedInteractionController")
        let recognizer = UIPanGestureRecognizer(target: target, action: Selector(("handleNavigationTransition:")))
        self.view.addGestureRecognizer(recognizer)
    }
}
  1. Extensionに書いたsetSwipeBack()をViewDidLoad()あたりで呼んどきます
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setSwipeBack()
    }
}
  1. 完成
21
15
1

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
21
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?