ios 更改 UINavigationBar 后退按钮标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23133804/
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-08-30 23:58:06  来源:igfitidea点击:

Change UINavigationBar back button title

iosuinavigationcontrollertitleback-button

提问by S S

In my application I want to use 'Back' text as back button title for every viewcontroller. I have read so many posts on stackoverflow but got nothing.

在我的应用程序中,我想使用“后退”文本作为每个视图控制器的后退按钮标题。我已经阅读了很多关于 stackoverflow 的帖子,但一无所获。

I don't want to set leftbarbuttonitem.

我不想设置 leftbarbuttonitem。

Can anyone help me on this simple task.

任何人都可以帮助我完成这个简单的任务。

Thanks,

谢谢,

回答by rustylepord

Do this in the parent view controller not in the child

在父视图控制器中而不是在子视图控制器中执行此操作

Swift

迅速

navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

Objetive-C

目标-C

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

回答by Alex_Burla

self.navigationController.navigationBar.topItem.title = @"";

回答by P.L.

If you are using storyboard you can select the navigation item in the parent view controller and set the button text you want in 'Back Button' field. Remember to set this in the parentview controller, not in the child that is pushed.

如果您使用故事板,您可以在父视图控制器中选择导航项,并在“后退按钮”字段中设置所需的按钮文本。请记住在视图控制器中设置它,而不是在被推送的子视图中设置。

enter image description here

在此处输入图片说明

回答by morroko

Try this hope it will be work

试试这个希望它会起作用

UIBarButtonItem *btn = 
        [[UIBarButtonItem alloc] initWithTitle:@"New Title" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:btn];

回答by Brian

I needed to use self.navigationController.navigationBar.backItem.title = @"";, the difference being that I'm using backIteminstead of topItem.

我需要使用self.navigationController.navigationBar.backItem.title = @"";,不同之处在于我使用的是backItem而不是topItem.

回答by rjobidon

Back Button With Back Arrow

带后退箭头的后退按钮

Objective-C

目标-C

self.navigationController.navigationBar.topItem.backBarButtonItem = 
[[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain
target:nil action:nil];

Swift

迅速

self.navigationController?.navigationItem.backBarButtonItem = 
UIBarButtonItem(title:"Title", style:.plain, target:nil, action:nil)

Normal Button Without Back Arrow

没有后退箭头的普通按钮

Objective-C

目标-C

self.navigationItem.leftBarButtonItem = 
[[UIBarButtonItem alloc] initWithTitle:@"Title" 
style:UIBarButtonItemStylePlain target:nil action:nil];

Swift

迅速

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Title", 
style:.plain, target:nil, action:nil)

Bold Button Without Back Arrow

无后退箭头的粗体按钮

Objective-C

目标-C

self.navigationItem.leftBarButtonItem = 
[[UIBarButtonItem alloc] initWithTitle:@"Title" 
style:UIBarButtonItemStyleDone target:nil action:nil];

Swift

迅速

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Title", 
style:.done, target:nil, action:nil)

回答by WoShiNiBaBa

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

回答by A.G

swift 2.0:

快速 2.0:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.topItem?.title = ""
}

Note: It works only if storyboard have a chain of navigation stack.

注意:仅当情节提要具有导航堆栈链时才有效。

Other options/changing title:

其他选项/更改标题:

self.navigationController?.navigationBar.backItem?.title = ""
navigationItem.backBarButtonItem?.title = ""
navigationItem.leftBarButtonItem?.title = ""

Removing navigationItem:

删除导航项:

navigationItem.setLeftBarButtonItem(nil, animated: true)

回答by PMW

Changes the currently visible Back Button

更改当前可见的后退按钮

extension UIViewController {
func setCurrentBackButton(title: String) {
    guard let vcCount = self.navigationController?.viewControllers.count else {
        return
    }

    let priorVCPosition = vcCount - 2

    guard priorVCPosition >= 0 else {
        return
    }

    self.navigationController?.viewControllers[priorVCPosition].navigationItem.backBarButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
}

回答by Abedalkareem Omreyh

To remove back button title for all view controller add new swift file and copy this extinction to it

要删除所有视图控制器的后退按钮标题,请添加新的 swift 文件并将此灭绝复制到它

    import UIKit

    extension UIViewController {
        static func swizzle(){


            let orginalSelector = #selector(viewDidLoad)
            let swizzledSelector = #selector(swizzledViewDidLoad)

            let orginalMethod = class_getInstanceMethod(UIViewController.self, orginalSelector)
            let swizzledMethod = class_getInstanceMethod(UIViewController.self, #selector(swizzledViewDidLoad))

            let didAddMethod = class_addMethod(UIViewController.self, orginalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))

            if didAddMethod {
                class_replaceMethod(UIViewController.self, swizzledSelector, method_getImplementation(orginalMethod!), method_getTypeEncoding(orginalMethod!))
            }else{
                method_exchangeImplementations(orginalMethod!, swizzledMethod!)
            }

        }

        @objc func swizzledViewDidLoad(){
            navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
            swizzledViewDidLoad()
        }


    }

After that in AppDelegate inside didFinishLaunchingWithOptions, call the swizzle function.

之后在AppDelegate里面didFinishLaunchingWithOptions,调用swizzle函数。

UIViewController.swizzle()

This function is using the objective c runtime to exchange the viewDidLoadmethod with another one that removes the back button title and then inside it recall the original viewDidLoad.

此函数使用目标 c 运行时viewDidLoad与另一个删除后退按钮标题的方法交换方法,然后在其中调用原始viewDidLoad.