我在使用 UINavigationController 的 UIViewController 中编写了以下代码。
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
}
我构建并运行我的应用程序,
self.navigationItem.hidesBackButton = YES;
上面那个可以正常工作,但是
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
那个不行。
所以,我重写了下面的代码。
- (void)viewDidLoad {
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)]];
backBarButton.tintColor = [UIColor clearColor];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
self.navigationItem.leftBarButtonItem = backBarButton;
}
它工作正常。
不过,我想用第一个例子。 第一个清楚地表达了我想做什么。
有人知道吗?
最佳答案
在viewDidLoad
中,view controller还没有包含在navigation controller中,所以navigationController
属性是nil
,这就是为什么行无效。
也就是说,分配 UINavigationController
的 interactivePopGestureRecognizer
的委托(delegate)并不是一个好的做法(我很确定它应该分配给导航 Controller )。尝试在 viewWillAppear:
中禁用手势识别器:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
关于ios - self.navigationItem.hide Back Button = YES 并在 iOS 7 上使用滑动手势返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21410638/