简体   繁体   English

从继承的导航栏Swift 3中删除后退按钮文本

[英]Remove back button text from inherited navigation bar Swift 3

I want to know if its possible to remove the navigation bar back button text from an inherited navigation bar. 我想知道是否可以从继承的导航栏中删除导航栏后退按钮文本。 Presently my navigation bar shows "< ControllerName". 目前我的导航栏显示“<ControllerName”。 I want to simply show the "<" back icon. 我想简单地显示“<”后退图标。 I would also like to know how to show "< Back", and how to remove it completely. 我还想知道如何显示“<Back”,以及如何完全删除它。

I understand I could do this by adding a bar button item to the storyboard, however is there an easier way to do it? 我知道我可以通过向故事板添加一个条形按钮项来实现这一点,但是有更简单的方法吗?

Note, this code does not work: 注意,此代码不起作用:

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

You better custom back button for this task. 您最好为此任务自定义后退按钮。

but You also can do it in other ways. 但你也可以通过其他方式做到这一点。 Ex: You have ViewController1 , and ViewController2 (You push ViewController2 from ViewController1 ) 例如:你有ViewController1ViewController2 (你推ViewController2ViewController1

ViewController1 ViewController1

public class ViewController1: UIViewController {

    override public func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.title = "viewcontroller1 title"
    }

}

ViewController2 ViewController2

class ViewController2: UIViewController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // get previous view controller and set title to "" or any things You want
        if let viewControllers = self.navigationController?.viewControllers {
            let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
            previousVC?.title = "" // or previousVC?.title = "Back"
        }
    }

}

我认为这对你有用。

self.navigationItem.hidesBackButton = true

Solution suggested by @Prashant will remove the back button from navigation bar. @Prashant建议的解决方案将从导航栏中删除后退按钮。 To remove the title, use following: 要删除标题,请使用以下内容:

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

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

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