简体   繁体   中英

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". I want to simply show the "<" back icon. I would also like to know how to show "< Back", and how to remove it completely.

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 )

ViewController1

public class ViewController1: UIViewController {

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

}

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. To remove the title, use following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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