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"
Related
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:
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 want to know if its possible to remove the navigation bar back button text from an inherited navigation bar. Presently my navigation bar shows "< ControllerName". I want to simply show the "<" back icon. I would also like to know how to show "< Back", and how to remove it completely.
I understand I could do this by adding a bar button item to the storyboard, however is there an easier way to do it?
Note, this code does not work:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
You better custom back button for this task.
but You also can do it in other ways. Ex: You have ViewController1, and ViewController2 (You push ViewController2 from ViewController1)
ViewController1
public class ViewController1: UIViewController {
override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = "viewcontroller1 title"
}
}
ViewController2
class ViewController2: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// get previous view controller and set title to "" or any things You want
if let viewControllers = self.navigationController?.viewControllers {
let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
previousVC?.title = "" // or previousVC?.title = "Back"
}
}
}
I think this will work for you.
self.navigationItem.hidesBackButton = true
Solution suggested by #Prashant will remove the back button from navigation bar.
To remove the title, use following:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
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.
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
}
...
}