简体   繁体   English

如何删除并重新添加导航栏上的“后退按钮”

[英]How to remove and add back the “back button” on navigation bar

My view controller have a back button show on navigation bar(in a UINavigationController). 我的视图控制器的导航栏中(在UINavigationController中)显示了一个后退按钮。 There is a "edit" mode in current view. 当前视图中有一个“编辑”模式。 When into "edit" mode, I replace the leftBarButtonItem to a "Cancel" button, when exit the "edit" mode, I want to change the left button to "back button" again, how can I do it? 当进入“编辑”模式时,我将leftBarButtonItem替换为“取消”按钮,当退出“编辑”模式时,我想再次将左按钮更改为“后退按钮”,我该怎么做?

I try self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem, it just remove the "Cancel" button, but not get the "back button" back. 我尝试self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem,它只是删除了“取消”按钮,而没有使“返回按钮”返回。

Now I can create a new navigation item with the UI just like the back button, and set it to as leftBarButtonItem, but the position is not same with the native back bar button, and lost the swipe back gesture. 现在,我可以像使用后退按钮一样使用UI创建一个新的导航项,并将其设置为leftBarButtonItem,但是位置与本机后退按钮不同,并且失去了后退滑动手势。

Is there any way to get the native back bar button item back on the navigation bar? 有什么方法可以使导航栏中的本机后退按钮项恢复原状?

退出编辑模式时,只需进行以下设置:

self.navigationItem.leftBarButtonItem = nil;
  • In same view controller just add self.navigationItem.backBarButtonItem.title = @"Cancel"; 在同一视图控制器中,只需添加self.navigationItem.backBarButtonItem.title = @"Cancel"; in viewWillAppear . viewWillAppear
  • And add self.navigationItem.backBarButtonItem.title = @"Back"; 并添加self.navigationItem.backBarButtonItem.title = @"Back"; in your viewDidDisappear to set back button text of your class file .m . viewDidDisappear以设置类文件.m后退按钮文本。

To set Cancel : 设置取消:

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.backBarButtonItem.title = @"Cancel";
}

To set Back : 退回:

-(void)viewDidDisappear:(BOOL)animated
{
    self.navigationItem.backBarButtonItem.title = @"Back";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM