문제 설명
탐색 컨트롤러로 보기로 돌아가기 위해 자동 제스처를 끄는 방법은 무엇입니까? (How to turn off the automatic gesture to go back a view with a navigation controller?)
그래서 사용자가 화면의 맨 왼쪽(어느 방향에서든)을 스와이프하면(이것은 iOS7의 새로운 기능입니다. )
지금까지 다음을 사용하여 끄려고 시도했지만 소용이 없었습니다.
[self.navigationItem setHidesBackButton:YES];
NavigationController 자체의 초기화 내에서(대리자가 이를 사용하는 것처럼 보임)
참조 솔루션
방법 1:
obj‑c
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
swift
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
방법 2:
Adding to Gabriele's Solution.
To support any iOS before iOS 7 you will need to wrap this code with this:
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
This will stop the App crashing in iOS 6 and iOS 5 for missing selector.
방법 3:
I use this solution in my project, it disables only interactivePopGestureRecognizer and allows you to use another gesture recognizers.
‑ (void)viewDidLoad {
[super viewDidLoad];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
‑ (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
return NO;
} else {
return YES;
}
}
방법 4:
I found out setting the gesture to disabled only doesn't always work. It does work, but for me it only did after I once used the backgesture. Second time it wouldn't trigger the backgesture.
Fix for me was to delegate the gesture and implement the shouldbegin method to return NO:
‑ (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Disable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
‑ (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Enable iOS 7 back gesture
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
‑ (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return NO;
}
방법 5:
For IOS 8 (Swift):
class MainNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.interactivePopGestureRecognizer.enabled = false
// Do any additional setup after loading the view.
}
}
(by Danoli3、Gabriele Petronella、Danoli3、Userich、Antoine、ilan)