Nice programing

Swift에서 뒤로 스 와이프 제스처 비활성화

nicepro 2020. 12. 6. 22:04
반응형

Swift에서 뒤로 스 와이프 제스처 비활성화


잠시 여기를 둘러 보았지만 작동하는 해결책을 찾지 못하는 것 같습니다.

Swift에서 이전보기 제스처로 돌아 가기 위해 스 와이프를 비활성화하려고합니다.

다음을 포함한 다양한 솔루션을 시도했습니다.

self.navigationController?.interactivePopGestureRecognizer.enabled = false

self.navigationController.interactivePopGestureRecognizer.delegate = self

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {
    return false
}

이 작업을 수행하는 새로운 방법이나 작동하는 다른 방법이 있습니까?


비활성화 할 수는 있지만 대부분의 iOS 사용자는 뒤로 버튼을 눌러 스 와이프하여 뒤로 돌아 가기 때문에 권장되지 않습니다. 비활성화하려면 modal segue전송 규모가 크지 않은 푸시 세그 대신를 사용하는 것이 더 합리적 입니다. 뒤로 가기 기능으로 스 와이프를 제거하려면 뒤로 버튼을 비활성화하고 화면 오른쪽 상단에 완료 버튼이 있습니다.

self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false;

다음은 뒤로 스 와이프를 비활성화하고 다시 활성화하는 쉬운 방법입니다.

Swift 3.x 이상

viewDidLoad / willAppear / didAppear 메서드에서 다음을 추가합니다.

navigationController?.interactivePopGestureRecognizer?.isEnabled = false

그냥 당신이 함께 할 경우 것을 명심 viewDidLoad, 다음 뷰를 열 때, 그것은 그것이 당신의 스택에 남아 있는지 여부에 따라 설정되지 않을 수 있습니다.

당신이 그것을 떨어져 유지하려는 경우가 아니면, 당신은 뷰 중 하나를 통해 닫힐 때 그것을 다시 설정해야합니다 willMove(toParentViewController:)willDisappear. 에 귀하의 navigationController의지 viewDidDisappear가 없습니다. 너무 늦었습니다.

navigationController?.interactivePopGestureRecognizer?.isEnabled = true

SplitViewControllers 에 대한 특별 참고 사항 :

CompC가 주석에서 지적했듯이 두 번째 탐색 컨트롤러를 호출하여 다음과 같이 세부 정보보기에 적용해야합니다.

navigationController?.navigationController?.interactivePopGe‌​stureRecognizer?.isE‌​nabled = false

Swift 2.2 및 Objective-C

Swift 버전 2.x 이하 :

navigationController?.interactivePopGestureRecognizer?.enabled

목표 -C :

self.navigationController.interactivePopGestureRecognizer.enabled

gestureRecognizerShouldBegin에서 false를 반환하여이 작업을 수행 할 수있었습니다.

class ViewController2: UIViewController, UIGestureRecognizerDelegate {
...
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.navigationController?.interactivePopGestureRecognizer.delegate = self
}

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    return false
}

Hari 또는 Stefan의 답변에 문제가 없지만 이것은 더 간결합니다. viewDidLoad에 넣으면 완료됩니다.

if navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
    navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer)
}

편집하다:

한 가지 작은주의 사항은 탐색 컨트롤러가 다른보기에서 열리고 탐색 컨트롤러가 닫히면 EXC_BAD_ACCESS 오류가 발생한다는 것입니다. 이를 수정하려면 원래 UIGestureRecognizer를 저장하고 뷰를 종료 할 때 다시 넣어야합니다.

알리다:

private var popGesture: UIGestureRecognizer?

제스처를 제거하기 직전 :

popGesture = navigationController!.interactivePopGestureRecognizer

그런 다음보기를 닫을 때 :

If popGesture != nil {
    navigationController!.view.addGestureRecognizer(popGesture!)
}

목적 -c

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:true];

  self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}

I generally make sure that swipe back is enabled in as many places as possible, even adding a custom gesture recognizer to add it to modal screens. However for an authentication and download process in my app I start the process with a modal navigation controller and then push the view for each next step. However, once it's completed I want to prevent them from backing up into the authentication screens.

For this scenario I've been using:

navigationController?.interactivePopGestureRecognizer?.isEnabled = false
navigationItem.hidesBackButton = true

in viewWillAppear() on the final screen. You can undo these in viewWillDisappear() if you're pushing another view and need them there.


RowanPD's logic for Swift 4

private var popGesture: UIGestureRecognizer?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if navigationController!.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) {
        self.popGesture = navigationController!.interactivePopGestureRecognizer
        self.navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer!)
    }

}

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

    if let gesture = self.popGesture {
        self.navigationController!.view.addGestureRecognizer(gesture)
    }

}

This is something you missed if it doesn't work after you tried all.

  1. Add navigationController?.interactivePopGestureRecognizer?.isEnabled = false to your viewWillAppear(animated:) method.
  2. if it doesn't work, remove navigation delegate from the view controller. Check again if your view controller is confirming UINavigationControllerDelegate, UIGestureRecognizerDelegate protocols. if so, just remove it.

If requirement is to show side menu on some of the screens then add AddScreenEdgePanGesture on this specific view instead of navigationController view

replace it

SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController?.view)

with this

SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.view)

참고URL : https://stackoverflow.com/questions/31731751/disable-swipe-back-gesture-in-swift

반응형