In my current project, my navigation controller title doesn't seem to update / change. To see what's wrong I've created a new project and embedded a navigation controller to the view controller.
override func viewDidAppear(animated: Bool) {
print(navigationController) // Optional(<UINavigationController: 0x7b356200>)
navigationController?.navigationItem.title = "Main"
navigationController?.navigationBar.backgroundColor = UIColor.greenColor()
}
Strangely the navigation bar changes to green but still there is no title. What could be the cause of this?
Replace your line with this
navigationController?.navigationBar.topItem?.title = "Main"
Related
My problem is exactly as explained in this question ViewController title color will change change color when back button clicked
I've Navigation Controller -> View Controller (#1) -> Segue to another View Controller (#2)
View Controller #2 has a different title color.
When I click on Back button in View Controller #2 to go back to Home View (#1) it's title color is not changed to what it should be but it remain the title colour of view controller #2.
I've set the correct title color in attribute inspector. I'm also setting title color exclusively in Home View Controller's viewWillAppear but it's colour is still not changed.
I'm wondering what else I need to do? Is there
I've already added code to set title color to white in Home View Controller's viewWillAppear
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(animated)
print("HomeViewController: viewWillAppear()")
// Mark: - Set navigation bar title color
setTitleColorWhite(vc: self)
activityIndicator.startAnimating()
fetchDetails()
}
And this is setTitleColorWhite
// Mark: - Set navigation bar title color
func setTitleColorWhite(vc: UIViewController){
let attrs = [
NSAttributedStringKey.foregroundColor: UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0),
NSAttributedStringKey.font: UIFont(name: "SFProText-Medium", size: 17.0)!
]
vc.navigationController?.navigationBar.titleTextAttributes = attrs
}
Update the color in HomeViewController's viewDidAppear instead of viewWillAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setTitleColorWhite(vc: self)
}
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"
}
My Tab Bar Controller controls 5 view controllers and I want that in those 5 main pages all the back buttons are disabled and not visible. How can I do that correctly? I have tried all Swift commands seen here in SO but none has worked up to now.
I have tried with
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
}
override func viewWillAppear(animated: Bool) {
self.navigationItem.hidesBackButton = true
}
but they don't work. I have also tried with
self.tabBarController?.navigationItem.hidesBackButton = true
but this is the strange result
To remove the "back" button from the navigation bar, you can create a UITabBarController class for your UITabBarController in the storyboard, and in that class, inside the ViewDidLoad() method, you can call
self.navigationItem.hidesBackButton = true
This will remove the back button.
The back button is probably added by the navigationcontroller of the tabbarcontroller. So you will have to check the tabbar navigation controller.
Something like this:
self.tabbarcontroller.navigationcontroller.navigationitem.hidesBackButton = true
I think this will resolve the issue. Since the backbutton normally is added by a navigationcontroller, and not by a tabbarcontroller
Update
I have recommended him that he should loose the navigation controllers after the tabbarcontroller. Since the tabbar already implements the navigation needed between the different views. This and hidesbackbutton = true solved his issue
I want to change the navigation's title of a view, which is related to a Tab Bar Controller, which is related to a Navigation Controller (see the picture)
I don't know how to do that.
With a basic view, I just need to do that in the ViewController.swift :self.title="test"
But here, this line changed the tab bar's title, but I want to change the navigation's title.
Main.Storyboard :
On Swift 3, in the UIViewController, override your viewDidAppear method and add this code snippet:
if let tabController = self.parent as? UITabBarController {
tabController.navigationItem.title = "My Title"
}
Use need to use this property:
self.navigationItem.title = "someTitle"
According to Apple best practices you should not have a tab bar controller contained inside of a navigation controller, rather you should have the view controller for each tab that requires one to be inside of it's own navigation controller.
There are various issues that can arise from having a tab bar controller contained within a navigation controller.
When implemented according to their standards you can set the title using self.title
An app that uses a tab bar controller can also use navigation controllers in one or more tabs. When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers.
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html
Embed UINavigationController in your storyboard and put:
navigationBar.topItem.title = "Nav title"
I also had difficulty to change a navigation bar title of a child view controller. The solution was:
#IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.topItem!.title = "Pickup Address"
}
For Swift 3+
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "Title"
}
I have a main page, which has a button that will go to another page.
I want when i go the second page, to have a back button at the top left of the navigation bar.
I added a navigation controller to the main view controller, and then i added a push segue to my second view controller (second page), so an automatic back button created.
what i want is that i do NOT want the navigation bar to be in the main view controller, and I don't want it to be in the second view controller, I just want to have the back button in the second view controller.
I thought about it and i came up doing:
self.navigationController?.setNavigationBarHidden(true, animated: false)
in the main view controller, that actually hide the navigation, but that makes the second view ctonroller to loose its back button.
do you have any solution to my problem?
this is the main view controller (which has the navigation bar, but i would like to not have it)
this is the second view controller, which has the back button,
I have no problem if i leave the navigatino bar, if it was transparent, any idea please? (and by transparent, i mean i can see my image bellow it)
Update 1
after the first comment gives me a hint, i tried to applied it like this:
class CustomNavigationBar: UINavigationBar {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
}
}
and I set the class of my navigation bar in the UINavigationControlelr to my custom navigation bar.
and in my main view controlelr i add this:
self.navigationController?.navigationBar.translucent = true;
self.navigationController?.navigationBar.backgroundColor = UIColor.clearColor()
but the result is that my Main view controller, still has a place (though it is empty) for the navigation bar. can't i make this place as transparent to see the image bellow it?
add this to the main view controller
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}