Pan Mluvčí
Pan Mluvčí

Reputation: 1261

Swift change Back button title after button tap

I have a VC that have some backbutton title "XYZ", in viewDidLoad I will change it to "ABC". Then I want to change it again with same method, but it is not working. Tried many solutions but nothing work.

I found this code. Woking in viewdidload...but after that it dont.

let backButton = UIBarButtonItem(
        title: " ",
        style: UIBarButtonItemStyle.Plain,
        target: nil,
        action: nil
    )
self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton

enter image description here

Upvotes: 1

Views: 453

Answers (1)

rollingcodes
rollingcodes

Reputation: 16062

Do this in your UIViewController class. This leaves the back arrow but removes the text beside it.

private var _title: String?
var backBarButtonHidden: Bool {
    didSet {
        if let index = navigationController?.viewControllers.indexOf(self) {
        if index > 0 {
            if let button = navigationController?.navigationBar.items?[index - 1] {
                if backBarButtonHidden {
                    _title = button.title
                   button.title = ""
                } else { button.title = _title }
            }
        }
     } 
}

Upvotes: 1

Related Questions