ios - self.navigationItem.hide Back Button = YES 并在 iOS 7 上使用滑动手势返回

标签 ios objective-c cocoa uinavigationcontroller uinavigationitem

我在使用 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,这就是为什么行无效。

也就是说,分配 UINavigationControllerinteractivePopGestureRecognizer 的委托(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/

相关文章:

objective-c - iOS 多人游戏

ios - 在 Swift 中根据条件使用调度队列的最佳方法

ios - 正则表达式来验证具有特定字符的字符串

ios - Xcode SSL 固定信任 anchor 证书

macos - 无法签署 Mac 安装程序以便在 Mac 应用商店之外分发

cocoa - 从一个 TableView 移动到另一个 TableView

iOS 8 beta - 位置管理器不注册用户位置

ios - 核心剧情: Make x-axis ticks evenly spaced

iphone - 如何确定 UITextField 中删除的位置

c# - 使用 "POST"方法从 .net 服务器(C# 和 json 格式)获取响应到 Objective-c 问题