Navigation Bar Back Button

Hi

Right now I have 2 views.

1) Main page contains Login Button (When clicked, it will go to Login Page)

2) Login page with back button on navigation bar.

The main page is set to hide nav bar. This works fine with this code

Code Block
self.navigationController?.navigationBar.isHidden = true


Now, when I click the login button on the main page, it goes to the login page.

In the login page I tried to rename the back button. But it does not work. This is the code that I used:

Code Block
     navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil,
            action: #selector(didTapBack)
            )
      
        navigationItem.backBarButtonItem?.title = "hohoho"


It still shows the back button named as Back

Now, when I tap the back button, it goes back to the main page. which is correct. However, at the main page it also displays the navigation bar without the back button. How can I hide the navigation bar when the user returns to the main page after clicking the back button?

When you show your code, please clarify where these lines exist -- method, class or some others.

How can I hide the navigation bar when the user returns to the main page after clicking the back button?

Please try this:
Code Block
class MainPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backButtonTitle = "hohoho"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
}
}


The way I do this usually:
  • create a UIBarButtonItem in IB in the "back place"

Code Block
    @IBOutlet fileprivate weak var backButton: UIBarButtonItem!

Then set in viewDidLoad

Code Block
backButton.tintColor = defaultBlueColor // if needed ; I defined defaultBlueColor elsewhere
backButton.title = NSLocalizedString("Come back", comment: "")

Navigation Bar Back Button
 
 
Q