Hi, I'm currently working on changing navigation bar back button in run time. However it doesn't work.
Below is the code where i want to change my back button title.
What I want to do is if I scroll more than hightCheck i want to make my backButton title to be "back", else " ".
The method for changing back button title seems to work in the first time when the view loads up, but does not change after all.
Is it to change back Button title in run time?
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let a = tableView.tableHeaderView?.subviews[1]
let heightCheck = tableView.bounds.origin.y + a!.frame.height + navigationController!.navigationBar.frame.height
var backButtonTitle = " "
if heightCheck > a!.frame.origin.y{
backButtonTitle = "back"
}else{
backButtonTitle = " "
}
navigationController?.navigationBar.topItem?.backButtonTitle = backButtonTitle
}
Below is the code where i want to change my back button title.
What I want to do is if I scroll more than hightCheck i want to make my backButton title to be "back", else " ".
The method for changing back button title seems to work in the first time when the view loads up, but does not change after all.
Is it to change back Button title in run time?
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let a = tableView.tableHeaderView?.subviews[1]
let heightCheck = tableView.bounds.origin.y + a!.frame.height + navigationController!.navigationBar.frame.height
var backButtonTitle = " "
if heightCheck > a!.frame.origin.y{
backButtonTitle = "back"
}else{
backButtonTitle = " "
}
navigationController?.navigationBar.topItem?.backButtonTitle = backButtonTitle
}
What I do usually:
create a Bar Button Item that I put in the left place.
Connect to an IBOutlet
Code Block @IBOutlet fileprivate weak var backButton: UIBarButtonItem!
Then change its title:
Code Block backButton.title = heightCheck > a!.frame.origin.y ? NSLocalizedString("back", comment: "") : ""