0

So I have 3 View controllers : V1, V2 and V3. When I press a button on V1 it shoots me over to V3. I would like to keep the "Back Arrow" but change the text there. I have tried many things but the word back either reapers when the view is shown or my code does not work.

Any Suggestions?

I already used this code

self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "YO", style: .Plain, target: nil, action: nil)

let backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Image can be downloaded from here below link
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)

func backAction() -> Void {
    self.navigationController?.popViewControllerAnimated(true)
}

I tried creating my Button with this code I found on Stack But the button disappeared and the back button appeared in its place again.

6
  • 2
    The text that appears for the back button is set by the title of the previous view controller Commented Dec 23, 2015 at 6:16
  • 1
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil) try this with in viewDidLoad and already there are lot of question related to this. Commented Dec 23, 2015 at 6:16
  • 1
    @Paulw11 Yeah I am aware of that. When the view shows up the new text appears there but then all the sudden the words Back replaces it. Commented Dec 23, 2015 at 6:19
  • 1
    @Bunty Madan I tried this but it isn't working in my case. I don't know if it has to do with the fact I am skipping V2? Commented Dec 23, 2015 at 6:21
  • 1
    @JakeC if possible then share your demo project Commented Dec 23, 2015 at 6:23

1 Answer 1

1

You can set navigationItem.leftBarButtonItem to a button of your own design. That overrides backBarButtonItem, and iOS will not replace your text with "Back". But if you want an arrow you would have to design it yourself as part of your button.

In my code I set the button whenever the title is changed:

override var title:String? {
    get {
      return super.title
    }
    set {
      super.title = newValue
      if (navigationController?.childViewControllers.count > 1) {
        let backButton = // ... custom button
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
        backButton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
      } else {
        navigationItem.hidesBackButton = true
      }
Sign up to request clarification or add additional context in comments.

3 Comments

I actually tried doing that and the button pops up then disappears and the back button reappears again. I Updated my code above with the button I created
This answer works great. I just need to make a custom back button now. If you have time to do that please post it! Thanks
I started with this button tutorial. It's Obj-C but not hard to Swiftify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.