hachinoBlog

hachinobuのエンジニアライフ

iOS7でスワイプによる戻る機能を無効にする方法

iOS7からスワイプすることで戻る機能が標準になりました。
いくつかのアプリではこの機能を無効にしたいと思うものもあると思います。

やり方は各ViewControllerで

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

これでOK。

私の場合はカスタムのUINavigationControllerを使って全てのクラスを管理していたので、カスタムのUINavigationControllerクラスにUINavigationControllerDelegateのnavigationController: willShowViewController: animated:メソッドを実装しました。

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //スワイプによる戻るを無効にする(スワイプを少しして戻すとNavigationBarが存在しなくなる事象回避)
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
}