본문 바로가기
iOS

[iOS] Custom Navigation Controller Pop Gesture Swift

by thoonk: 2022. 9. 22.

커스텀 네비게이션 컨트롤러를 설정했을 때, Pop Gesture가 작동하지 않는 이슈 해결 방법을 기록합니다. 

 

기본적인 NavigationController를 사용했을 때, Swipe로 현재 뷰컨트롤러를 스택에서 Pop할 수 있습니다.

하지만, 네비게이션 바를 커스텀했을 때, Pop Gesture가 작동하지 않아 해결 방법을 찾게 되었습니다.

 

익스텐션을 활용하여 viewDidLoad 메서드에서 interactivePopGestureRecognizer의 Delegate를 설정합니다.

gestureRecognizerShouldBegin 메서드에서 스택에 2개 이상의 뷰컨트롤러, 즉 rootViewController를 제외한 뷰컨트롤러들이 있을 때 gestureRecognizer가 인식을 시작합니다. 

 

import UIKit

extension UINavigationController: UIGestureRecognizerDelegate {
    open override func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }
    
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return viewControllers.count > 1
    }
}

 

부족한 점 피드백해주시면 감사합니다👍

 

Ref.

https://stackoverflow.com/questions/34942571/how-to-enable-back-left-swipe-gesture-in-uinavigationcontroller-after-setting-le

 

How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?

I got the opposite issue from here. By default in iOS7, back swipe gesture of UINavigationController's stack could pop the presented ViewController. Now I just uniformed all the self.navigationItem.

stackoverflow.com

 

'iOS' 카테고리의 다른 글

[iOS] Crashlytics 적용  (0) 2023.01.03
[iOS] Tag을 활용한 UIButton Action 처리  (0) 2022.10.28
[iOS] 버튼 이미지가 표시되지 않는 이슈  (0) 2022.07.11
[iOS] WKWebView 정리 Swift  (0) 2022.02.04
[iOS] Unwind Segue  (1) 2021.11.11

댓글