iOS 8 UINavigationController禁用后退按钮

时间:2023-01-21 14:29:23

in my Navigation Controller I need to temporarily disable the back button. I know that it can be hidden using the following or something similar:

在我的导航控制器中,我需要暂时禁用后退按钮。我知道它可以使用以下或类似的东西隐藏:

[self.navigationController.navigationItem setHidesBackButton:YES animated:YES];

[self.navigationController.navigationItem setHidesBackButton:YES animated:YES];

But that is not what I need, instead I want the back button to be greyed out and non-responsive to user touch events. Is their a way to achieve this without replacing the default back button?

但这不是我需要的,而是我希望后退按钮变灰并且不响应用户触摸事件。他们是否可以在不更换默认后退按钮的情况下实现此目的?

Thanks in advance!

提前致谢!

4 个解决方案

#1


15  

To disable the back button, these commands would make it do what you want it to do:

要禁用后退按钮,这些命令会使它按照您的意愿执行:

Enable:

启用:

self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blueColor];

Disabled:

禁用:

self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

Update:

更新:

As of iOS 7, there's also a swipe that you'll want to disable on the UINavigationBar.

从iOS 7开始,您还需要在UINavigationBar上禁用滑动。

// You wrap it an 'if' statement so it doesn't crash
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
// disable the interactivePopGestureRecognizer
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

#2


7  

This hides the back button, sot it becomes unreachable for the user. But not disables it:

这隐藏了后退按钮,使用户无法访问它。但不禁用它:

[self.navigationItem setHidesBackButton:YES];

#3


6  

I know this is quite old but I had this problem too.

我知道这已经很老了,但我也有这个问题。

In my case in one scenario I had to disable the back button and in another one I had to disable all navigation buttons. my solution was disabling the navigation bar in total in both scenarios:

在我的情况下,在一个场景中,我不得不禁用后退按钮,而在另一个场景中,我必须禁用所有导航按钮。我的解决方案是在两种情况下总共禁用导航栏:

self.navigationController.view.userInteractionEnabled = NO;

This won't show the buttons as disabled but will prevent touches.

这不会将按钮显示为已禁用但会阻止触摸。

Hope this will help

希望这会有所帮助

#4


4  

I believe that following should help:

我认为以下应该有所帮助:

self.navigationController.navigationItem.backBarButtonItem.enabled = NO;

UPDATE

UPDATE

Sorry guys, my belief didn't come true.

对不起伙计们,我的信念没有实现。

It seems that property backBarButtonItem is designed only for setting custom title or image for Back Button.

似乎属性backBarButtonItem仅用于为Back Button设置自定义标题或图像。

From documentation:

从文档:

If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

如果要为后退按钮指定自定义图像或标题,则可以将自定义栏按钮项(带有自定义标题或图像)指定给此属性。配置条形按钮项时,不要为其指定自定义视图;无论如何,导航项忽略后栏按钮中的自定义视图。

The default value of this property is nil.

此属性的默认值为nil。

Unfortunately I didn't find any way of disabling back button with saving its native look and behaviour, because any time when I try to set custom UIBarButtonItem into navigationItem.backBarButtonItem property - it gets updated with appropriate native back button style and it always has enabled == YES.

不幸的是,我没有找到任何方法来禁用后退按钮并保存其原生外观和行为,因为任何时候我尝试将自定义UIBarButtonItem设置为navigationItem.backBarButtonItem属性 - 它会使用适当的本机后退按钮样式进行更新,并且始终启用==是的。

I think this is done by Apple for a reason because we basically shouldn't force the user to stay on a detail screen and disable him from going back. Also, in iOS7 and later user always can use swipe-from-left-edge gesture (if you don't disable it) to go back.

我认为这是由Apple完成的,因为我们基本上不应强迫用户留在细节屏幕上并禁止他回去。此外,在iOS7及更高版本中,用户始终可以使用从左侧滑动手势(如果您不禁用它)返回。

The only one ugly thing that I can recommend is to create a custom UIBarButtonItem and set it into leftBarButtonItem with 'Back' title, target and selector which will pop your viewController. By default it will substitute native back button. Then you can disable it as usual using navigationItem.leftBarButtonItem.enabled = NO.

我可以推荐的唯一一个丑陋的事情是创建一个自定义的UIBarButtonItem并将其设置为leftBarButtonItem,其中“Back”标题,目标和选择器将弹出您的viewController。默认情况下,它将替换本机后退按钮。然后你可以像往常一样使用navigationItem.leftBarButtonItem.enabled = NO禁用它。

Unfortunately it will not look and act (in case of title updating depending on available space) as native back button :(

不幸的是它不会看起来和行为(如果标题更新取决于可用空间)作为本机后退按钮:(

#1


15  

To disable the back button, these commands would make it do what you want it to do:

要禁用后退按钮,这些命令会使它按照您的意愿执行:

Enable:

启用:

self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blueColor];

Disabled:

禁用:

self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

Update:

更新:

As of iOS 7, there's also a swipe that you'll want to disable on the UINavigationBar.

从iOS 7开始,您还需要在UINavigationBar上禁用滑动。

// You wrap it an 'if' statement so it doesn't crash
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
// disable the interactivePopGestureRecognizer
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

#2


7  

This hides the back button, sot it becomes unreachable for the user. But not disables it:

这隐藏了后退按钮,使用户无法访问它。但不禁用它:

[self.navigationItem setHidesBackButton:YES];

#3


6  

I know this is quite old but I had this problem too.

我知道这已经很老了,但我也有这个问题。

In my case in one scenario I had to disable the back button and in another one I had to disable all navigation buttons. my solution was disabling the navigation bar in total in both scenarios:

在我的情况下,在一个场景中,我不得不禁用后退按钮,而在另一个场景中,我必须禁用所有导航按钮。我的解决方案是在两种情况下总共禁用导航栏:

self.navigationController.view.userInteractionEnabled = NO;

This won't show the buttons as disabled but will prevent touches.

这不会将按钮显示为已禁用但会阻止触摸。

Hope this will help

希望这会有所帮助

#4


4  

I believe that following should help:

我认为以下应该有所帮助:

self.navigationController.navigationItem.backBarButtonItem.enabled = NO;

UPDATE

UPDATE

Sorry guys, my belief didn't come true.

对不起伙计们,我的信念没有实现。

It seems that property backBarButtonItem is designed only for setting custom title or image for Back Button.

似乎属性backBarButtonItem仅用于为Back Button设置自定义标题或图像。

From documentation:

从文档:

If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

如果要为后退按钮指定自定义图像或标题,则可以将自定义栏按钮项(带有自定义标题或图像)指定给此属性。配置条形按钮项时,不要为其指定自定义视图;无论如何,导航项忽略后栏按钮中的自定义视图。

The default value of this property is nil.

此属性的默认值为nil。

Unfortunately I didn't find any way of disabling back button with saving its native look and behaviour, because any time when I try to set custom UIBarButtonItem into navigationItem.backBarButtonItem property - it gets updated with appropriate native back button style and it always has enabled == YES.

不幸的是,我没有找到任何方法来禁用后退按钮并保存其原生外观和行为,因为任何时候我尝试将自定义UIBarButtonItem设置为navigationItem.backBarButtonItem属性 - 它会使用适当的本机后退按钮样式进行更新,并且始终启用==是的。

I think this is done by Apple for a reason because we basically shouldn't force the user to stay on a detail screen and disable him from going back. Also, in iOS7 and later user always can use swipe-from-left-edge gesture (if you don't disable it) to go back.

我认为这是由Apple完成的,因为我们基本上不应强迫用户留在细节屏幕上并禁止他回去。此外,在iOS7及更高版本中,用户始终可以使用从左侧滑动手势(如果您不禁用它)返回。

The only one ugly thing that I can recommend is to create a custom UIBarButtonItem and set it into leftBarButtonItem with 'Back' title, target and selector which will pop your viewController. By default it will substitute native back button. Then you can disable it as usual using navigationItem.leftBarButtonItem.enabled = NO.

我可以推荐的唯一一个丑陋的事情是创建一个自定义的UIBarButtonItem并将其设置为leftBarButtonItem,其中“Back”标题,目标和选择器将弹出您的viewController。默认情况下,它将替换本机后退按钮。然后你可以像往常一样使用navigationItem.leftBarButtonItem.enabled = NO禁用它。

Unfortunately it will not look and act (in case of title updating depending on available space) as native back button :(

不幸的是它不会看起来和行为(如果标题更新取决于可用空间)作为本机后退按钮:(