xcode 导航栏标题和后退按钮名称相同吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15829358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:08:02  来源:igfitidea点击:

Navigationbar title and back button name are same?

iosxcodeuinavigationcontrolleruinavigationbar

提问by Naveen

I created one title for navigationbar in my storyboard project ,but when I moved to next viewcontroller the back button shows the same name as my previous navigationbar(Main navigationbar) title.? Can i set separate title and back button names?

我在故事板项目中为导航栏创建了一个标题,但是当我移动到下一个视图控制器时,后退按钮显示的名称与我以前的导航栏(主导航栏)标题相同。?我可以设置单独的标题和后退按钮名称吗?

I tried following code but its not working ? why?

我试过下面的代码,但它不工作?为什么?

 self.navigationItem.title=@"Recent Books";
self.navigationItem.backBarButtonItem.title=@"Back";

回答by βhargav?

According to UINavigationItem Class Reference

根据UINavigationItem 类参考

"When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] 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."

“当此导航项紧邻堆栈中的顶部项下方时,导航控制器从该导航项派生导航栏的后退按钮。[...] 如果要为后退按钮指定自定义图像或标题,您可以将自定义栏按钮项目(带有您的自定义标题或图像)分配给此属性。”

So try this in your first VC from where you are pushing other VC

因此,在您推动其他 VC 的第一个 VC 中尝试此操作

self.navigationItem.title=@"Recent Books";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back"
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
self.navigationItem.backBarButtonItem=backButton;

回答by Ankur

Let's say you are pushing your viewcontroller A -> B

假设您正在推动视图控制器 A -> B

In viewcontroller A try adding this code in viewDidLoadmethod

在视图控制器中尝试在viewDidLoad方法中添加此代码

UIBarButtonItem *btn_Back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
 style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem=btn_Back;

So you will see your "Back" button in viewcontroller B

所以你会在视图控制器 B 中看到你的“返回”按钮

回答by Pratik

The following should solve your problem

以下应该可以解决您的问题

-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.backItem.title = @"back";
}

You can also use this Objective-C syntax

你也可以使用这个 Objective-C 语法

[[self.navigationController.navigationBar backItem] setTitle:@"back"];

回答by Ajay Sharma

Just do it in this way , one of the easiest way I did find till now :

就这样做吧,这是我迄今为止发现的最简单的方法之一:

Create a custom back button and add it into the Navigation Bar left button item property :

创建一个自定义后退按钮并将其添加到导航栏左按钮项属性中:

- (void)viewDidLoad
{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"Backnavigation.png"]  ; // Here set the back button image
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 24, 24);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
}


//Handle the Back Button Event
- (void) handleBack:(id)sender
{
    // do your custom handler code here
    [self.navigationController popViewControllerAnimated:YES];
}

Also, doing this way, you don't need to change the title of NavigationBar every-time.

此外,这样做,您不需要每次都更改 NavigationBar 的标题。

Here is the Back Button for your reference :enter image description here

这是后退按钮供您参考:在此处输入图片说明

回答by Najeeb ur Rehman

You can use the below code in the viewWillAppear(animated:Bool)method of the view controller in which you want to set the backButton title of you choice : self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "Back Title", style: .plain, target: nil, action: nil)

您可以在要设置您选择的 backButton 标题的视图控制器的viewWillAppear(animated:Bool)方法中使用以下代码: self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "Back Title", style: .plain, target: nil, action: nil)