更改UINavigationBar后退按钮标题

时间:2014-04-17 12:58:32

标签: ios uinavigationcontroller title back-button

在我的应用程序中,我想使用'Back'文本作为每个viewcontroller的后退按钮标题。 我已经在stackoverflow上阅读了这么多帖子但却一无所获。

我不想设置leftbarbuttonitem。

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

谢谢,

11 个答案:

答案 0 :(得分:98)

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

<强>夫特

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

<强> Objetive-C

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

答案 1 :(得分:33)

 $(document).ready(function(){
    var count=1;
    var nr=0;
    var pfad=[];
    $.ajax({
        method:"post",
        url:"phpcode.php",
        data:{func:"bilder"},
        success:function(data){
            var arr=[];
            images=data.split(";");
            länge=images.length;
            for(i=0;i<länge;i++){
                arr[i]=data.split(";")[i];
            }
            arr.pop();
            generate(arr);
        }
    });
    function generate(arr){
        länge=arr.length;
        for(i=0;i<länge;i++){
            if(i<1){
                $("main").append("<div value="+count+" class='pfade'><a>bild"+count+":</a><input name='bild' value="+arr[i]+" placeholder='pfad' type='text'/><button class='button' data-count="+count+" type='button'>X</button></div> ");
            count=count+1;
            }
            else{
                $("main").append("<div value="+count+" class='pfade'><a>bild"+count+":</a><input name='bild' value="+arr[i]+" placeholder='pfad' type='text'/><button class='button' data-count="+count+" type='button'>X</button></div> ");
                count=count+1;
            }
        }
    }

    $("#del").click(function(){
        val=count-1;
        $("div[value="+val+"]").remove();
        count=count-1;
    });

    $(document).on("click",".button",function(){
        val=$(this).attr("data-count");
        alert(val);
    });

    $("#more").click(function(){
       $("main").append("<div value="+count+" class='pfade'><a>bild"+count+":</a><input name='bild' value='' placeholder='pfad' type='text'/><button class='button' data-count="+count+" type='button'>X</button></div> ");
       count=count+1;
    }); 
    $("#sub").click(function(){
        var pfad=[];
        $("input[name='bild']").each(function(){
            if($(this).val()!= ""){
                pfad.push($(this).val());
            }
        });
        alert(pfad);
        $.ajax({
            url: "phpcode.php",
            method: "POST",
            data:{func:"addimg"},
            success:function(data){ 
            }
        });
    });
});

答案 2 :(得分:10)

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

enter image description here

答案 3 :(得分:5)

试试这个希望它会起作用

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

答案 4 :(得分:4)

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

答案 5 :(得分:1)

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

答案 6 :(得分:0)

swift 2.0:

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

注意:仅当故事板具有一系列导航堆栈时才有效。

其他选项/更改标题:

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

删除navigationItem:

navigationItem.setLeftBarButtonItem(nil, animated: true)

答案 7 :(得分:0)

更改当前可见的后退按钮

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)
}

答案 8 :(得分:-1)

要删除所有视图控制器的后退按钮标题,请添加新的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()
    }


}

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

UIViewController.swizzle()

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

答案 9 :(得分:-1)

AppDelegate的{​​{1}}中

添加此代码:

DidFinishLaunchingWithOptions

答案 10 :(得分:-1)

带有后退箭头的后退按钮

Objective-C

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

迅速

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

不带后退箭头的普通按钮

Objective-C

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

迅速

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

没有后退箭头的粗体按钮

Objective-C

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

迅速

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