I want to customize back button to show some different text than the title of previous viewController.
I tried:
self.navigationItem.backBarButtonItem!.title = "CustomText"
in ViewDidLoad() method in relevant viewController.
I read here backBarButtonItem in iOS (Swift) that left bar button works here, but in my case it returns nil.
Thanks for help in advance!
I agree with #Amit89 but I suggest adding self.navigationItem.title = "CustomText" under viewWillDisappear like this:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = "CustomText"
}
Otherwise, your back button title will disappear suddenly after the new view controller push animation finished.
Also, remember to reset the old view controller's title when it appears.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "Original Title"
}
The best place to do this title change is in its previous view controller'sviewDidDisappear method.
Ex: You want to see different title in ViewControllerB
ViewControllerA -> ViewControllerB.
In ViewControllerA's viewDidDisappear method,
override func viewDidDisappear(animated: Bool) {
self.navigationItem.title = "ABC"
}
So when it will go to ViewControllerB, you will see "ABC" as back button title. When you come back to ViewControllerA, you should restore the old title in viewWillAppear method of ViewControllerA.
Related
I have a UITableView embedded in a NavigationController. The cells each link out to a larger information ViewController. For UI purposes, I hide the Navigation Bar on the TableView and show it in the InfoViewController.
The problem I am experiencing is this: upon booting the app, the NavBar is successfully hidden on the TableView. The first time I tap into a cell and open an InfoViewController, the NavBar comes back as expected. I back out of that VC and into the TableView. Again, the NavBar is hidden, as expected. If I tap into another cell, the NavBar is not displayed as expected. NOTE: This happens even when I remove any code to hide the Navigation Bar.
Here are the relevant code snippets:
TableViewController (in ViewDidLoad()):
self.navigationController?.isNavigationBarHidden = true
InfoViewController:
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = false
super.viewWillAppear(animated)
}
Why would it work the first time, but not the second. Any help is appreciated!
For clarification:
App opens to TableView:
enter image description here
I click into the TableViewCell to Segue to InfoViewController:
enter image description here
I hit "Back" to go back to TableViewController. NavBar is still hidden. I click on the same cell:
enter image description here
EDITED: Messed up the TableViewController Code. Put = false instead of = true.
Also, I have one more thought, please someone check this for me. The TableViewController is inside a UIContainerView. It is almost as if when I hit "Back" I am exiting the NavigationController flow and I cannot get back in it.
Please try this code its working fine for hiding navigationBar
TableViewController
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
}
}
InfoViewController
class InfoViewController : UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = true
}
}
Simple hide navigationbar again when the view controller is appear again,
do below code in tableViewController:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = true
}
The sequence is tabBarController->navigationController->viewController->viewController
I wrote the following code in the second viewController.
When the user comes to the second viewController, I want to hide the tab bar.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tabBarController?.tabBar.isHidden = true
}
When the user clicks "Cancel" button, I want to go back to the main page.
#IBAction func cancelAction(_ sender: Any) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.
self.navigationController?.popToRootViewController(animated: true)
}
After go back to the first viewController, I want to make the tab bar show again.
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
But in fact, the tab doesn't show at all. And the page suddenly shakes for a moment. Don't know why.
Remove all your code for setting Tab bar, and try this option for Second View Controller in XIB or Storyboard:
There are few similar kind of questions asked and answered by many on Stackoverflow. But none of solution matches with my requirement.
I am using Swift3 for IOS mobile app development and used Navigation controller to manage the navigation. I gave title to all pages using below code.
self.title = "Title"
When I move to next page, then it shows me back button with the earlier page title. For some pages, title is long and it disturbs my header section of page.
Instead of page title, I want to change button text to "Back".
Any idea how to do that?
if you are using storyboard then you can set back button title there, click on ViewController -> Navigation Item -> Back Button and set "Back" title.
alternatively you can set title in ViewWillApear and change it to "Back" in viewWillDisappear method
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = "My Title"
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.title = "Back"
}
In your prepare function before the segue do this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let backItem = UIBarButtonItem()
backItem.title = "Back" // Change to desired title here
navigationItem.backBarButtonItem = backItem
}
Try to remove the title on viewWillDisapperar then re enter it on viewWillAppear
Try this:
self.navigationController?.navigationBar.topItem?.title = "Back"
It should work :)
Try this :
self.navigationController!.navigationBar.topItem!.title = "Back"
I was trying to set the back button title in a navigation bar, like this
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = self.backUpTitle
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.title = "Back"
}
Where self.backUpTitle has the original title for the current ViewController.
It works very well, but I'm a having a quick effect each time I click "Back": the title of the navigation bar appears with the first three letters followed by ellipsis (Eg: "Title" would show as "Tit..."), and after the view fully appears, it shows the entire title without any problem.
The thing is... it does not happen in a normal case, so I guess it has to do with my solution about setting Back Button Title.
The question is: is there a way to avoid this effect? Am I calling self.title in a wrong function?
I’m using Xcode 8 and iOS 10.0
I’ve tried running your code on my own machine and I’m not showing the same problem; I’m thinking you might be using custom views for the title of the navigation bar and your self.backUpTitle is inside a custom view that causes the ellipsis.
Some suggestions:
If you just want to show “Create User” that way without the ellipsis, you might want to remove all custom views for your navigation bar and just set the ViewController title like what you are doing in your code.
Using “self.title” will change the title of your ViewController, make sure your ViewController is embedded to a UIViewController. However, if you created your navigation bar, setting the title should be:
navigationBar.topItem.title = “Create User”
Just to reiterate, this is what my code looks like (which looks like yours) under a ViewController that is embedded in a UINavigationController:
var backUpTitle: String!
override func viewDidLoad() {
backUpTitle = "Create User"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = self.backUpTitle
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.title = "Back"
}
I have a question regarding the navigation bar.
As far as I understand from iOS: A view controller opened by a segue inherits the navigation bar of the parent view controller. Is this correct so far?
Is there a view controller within a stack "owns" the navigation bar in a complex segue stack (e.g. TableViewController that opens a TabBarController that opens ...)?
I very often run into the problem that I don't know where to get the actual navigation item in order to set the title or a bar button item.
In this case, I have the following controllers:
TabBarController
EventPostsViewController -> To display a list of posts, is a tabbed view within the TabBarController
CreatePostViewController -> To write a new post
So within the EventPostsViewController I can do this (and it works):
class EventPostsViewController: UITableViewController {
...
override func viewWillAppear(animated: Bool) {
...
// This solution works, but only for EventPostsViewController
self.tabBarController?.navigationItem.title = "text"
But within the CreatePostViewController, which is opened by a segue via EventPostsViewController, neither of this solutions work.
class CreatePostViewController: UIViewController {
...
override func viewWillAppear(animated: Bool) {
...
// Neither of these solutions works
self.navigationItem.title = "Text"
self.tabBarController?.navigationItem.title = "Text"
self.navigationController?.navigationItem.title = "Text"
How do I get the actual navigation bar/navigationItem?
Stupid simple mistake I repeat every time :)
I forgot to link my custom CreatePostViewController with the view controller using the interface builder.
This code now works:
class CreatePostViewController: UIViewController {
...
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.setNavigationBarHidden(false, animated: false)
// Set title
self.navigationItem.title = "Write Post"
// Add Submit button
var submitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "submitPost:")
self.navigationItem.rightBarButtonItem = submitButton
}
...
}