Search code examples
swiftnavigationtitleback-button

Swift 4 remove text from back button


Guys i want to remove text from Back button. i use this code to navigate from VC1 to VS2 (login form). I tried to add this code to VC2 but nothing.

self.navigationController?.navigationBar.backItem?.title = ""

code:

@IBAction func didSelectSegment(_ sender: UISegmentedControl) {
    
    let vcName = vcNames[sender.selectedSegmentIndex]
    if vcName == "NQTPastRecordViewController" && loggined() == false {
        let alert = alertAskLogin({
            let signInVC = getController("EVSignInViewController")
            
            self.navigationController?.pushViewController(signInVC, animated: true)
        })
        self.present(alert, animated: true, completion: nil)
        segmentControll.selectedSegmentIndex = 0
        return
    }
    self.container.swipeToController(at: sender.selectedSegmentIndex)
    
}

Solution

  • There are a couple ways to achieve what you want to do.

    When you push a view controller (VC2) onto the navigation stack for the first controller (VC1) the back button uses the title of the first screen by default as the back button title when the second screen is presented.

    enter image description here

    So in VC1 you could set the view controller's title to an empty string before you push onto the stack

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = ""
    }
    

    and this will leave you with a back button that just has the '<' character.

    enter image description here

    The second method is to set a custom back bar button item in the parent view controller (VC1)

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
    }
    

    enter image description here

    which sets the back button title to whatever you set in your UIBarButtonItem title field.

    So your implementation might look something like this: (pick one option or the other where commented, if you do both the custom bar button item overrides the title.)

    @IBAction func didSelectSegment(_ sender: UISegmentedControl) {
    
        let vcName = vcNames[sender.selectedSegmentIndex]
        if vcName == "NQTPastRecordViewController" && loggined() == false {
            let alert = alertAskLogin({
                let signInVC = getController("EVSignInViewController")
    
                // set title to empty string here
                title = ""
    
                // or set a custom back bar button item and set target/action as needed
                navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
    
                navigationController?.pushViewController(signInVC, animated: true)
            })
            self.present(alert, animated: true, completion: nil)
            segmentControll.selectedSegmentIndex = 0
            return
        }
        container.swipeToController(at: sender.selectedSegmentIndex)
    
    }