I am having an issue in my app with a particular back button changing font colour for no apparent reason. I have been through all of my code and I can't see any reason for it, but when launching my app and segueing to the problem view controller, the text colour seems to be set to the default blue colour.
If I tap to go to a different tab and then go back to this view controller, then the font returns to the correct colour (white) and this is despite not having any code in viewWillAppear/Disappear viewDidAppear/Disappear.
The code I am using to set the navigation bar text colour is:
override func awakeFromNib() {
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)!]
self.navigationController?.navigationBar.titleTextAttributes = attributes
}
And this is in the view controller that is segued from when going to the view controller with the back button. I have tried adding this code into viewWillAppear() but even this doesn't make a difference.
Does anyone know why, on launch, my app doesn't set the back button text colour and requires a tab switch for it to update?
Please Try to write same code in view will apear method.Than it should persist style to back button
I think this code will work
self.navigationController?.navigationBar.barTintColor = UIColor.someColor()
//This sets the background colour of the navigation bar
self.navigationController?.navigationBar.tintColor = UIColor.someColor()
// This sets the colour for navigation and bar button items
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "someFont", size: 20)!, NSForegroundColorAttributeName: UIColor.blackColor()]
//This sets the attributed text for the title of the navigation bar
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "someFont", size: 17)!], forState: .Normal)
//And this sets the attributed text for the bar button item(s) on the navigation bar (So that includes the back button)
Call it in the viewWillAppear
I hope this works :)
Related
My application has a setting screen that allows the user to change app color including navigation bar and tab bar tint colors and also allow him to change app font and font size for all texts in the app like in Telegram application. the problem is when user select a color for a list of specific colors I need something like reloads or refreshes my application to make the change affect the tab bar and navbar of the application
I found this question but it changes the color one time when the app launched but I want to allow the user to customize the application color and font
Changing navigation bar color in Swift
After a long search, I found the answer to fix the problem of the change navigation bar and tab bar color, tint color, and font in run time when the user chooses a specific color.
to change the color of the navigation bar:
use this to change the color of navbar for the current screen
navigationController?.navigationBar.barTintColor = Color
and that for changing the color for navbar in the entire app
UINavigationBar.appearance().tintColor = Color
UINavigationBar.appearance().barTintColor = Color
and this for changing navbar font for the current screen
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: AppFont().large]
and this for the entire app
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: AppFont().large]
and this for change tab bar tint color
self.tabBarController?.tabBar.tintColor = Color.theme.value
and this to change tab bar items font
let selectedAttrs = [NSAttributedString.Key.font: Font, NSAttributedString.Key.foregroundColor: Color]
if let items = self.tabBarController?.tabBar.items {
for item in items {
item.setTitleTextAttributes(selectedAttrs, for: .selected)
}
}
https://gph.is/g/ZOR7bAP
I'm trying to realize navigation bar and status bar that take colors of the image on top of controller. I've try with this two code:
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
but it change only the main controller and I need to make invisible only the second controller not the main. Here is an image of what I want exactly.
If you want the color of the image just put the image on top and later a visual effect view.
You can make the navigation bar transparent with this extension by calling:
navigationController?.navigationBar.apply(.transparentWhite)
in the viewWillAppear of any view controller in which you want this behaviour.
If you want other themes, define them as I defined the one in the gist:
static var transparentWhite: NavigationTheme { return NavigationTheme(attributes: [.font: UIFont(name: .avenirNextRegular, size:14.0)], barColor: .clear, tintColor: .white) }`
I've adjusted the default tint of my navigation bar to be white and it appropriately adjusts the color of each of my navigation bar elements:
However, when I push to a new view controller and try to set the title property, the tint is no longer applied:
I know that I can supply a label or something similar to the titleView attribute of my view controller that would do the trick, but that's a lot of work (relatively speaking) and in my mind the text should just default to the navigation bar's tint color. Am I missing something else? Or is this standard behavior that requires a custom titleView to override?
The tint property does not affect the color of the title. To set the title color (along with other attributes like font) globally, you can set the titleTextAttributes property of the UINavigationBar appearance to suit your needs. Just place this code in your AppDelegate or somewhere else appropriate that gets called on launch:
Swift 3:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Swift 2
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
No you work correctly. But you should to set color for second view. You can use this code to solve your problem.
In second view write this code to set color and font for your navigation title.
navigationController!.navigationBar.titleTextAttributes = ([NSFontAttributeName: UIFont(name: "Helvetica", size: 25)!,NSForegroundColorAttributeName: UIColor.white])
There is some lag when switching navbar title fonts between VCs, any help appreciated.
On my mainVC I set the navbar title font in viewDidLoad as:
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Billabong", size: 27)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
And, to switch back to the normal font on the other following VCs, I change the font back to normal on the mainVC viewWillDisappear as:
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Medium", size: 17)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
Up to here everything is fine, and the newVC shows the original font just fine, but when going back to the mainVC the font change has some lag. The way I'm doing it is by using the viewWillDisappear from the newVC displayed, however when it returns to the mainVC it shows half the title with the new font followed by ... and after some short lag it displays the rest of the title (eg "CoolTi..." and then "CoolTitle").
I've tried using viewWillAppear on the mainVC and same thing happens, also tried viewDidAppear on the mainVC and the lag on font change is even worse. Not sure how to switch back to other font without the lag, any ideas? Thanks
[SOLVED] Thanks #warrenm:
using navigationItem.titleView for the mainVC title instead and no lag when switching fonts between VCs
code:
let coolTitle = UILabel()
coolTitle.attributedText = NSAttributedString(string: "coolTitle", attributes: [NSFontAttributeName: UIFont(name: "Billabong", size: 27)!, NSForegroundColorAttributeName: UIColor.whiteColor()])
coolTitle.sizeToFit()
navigationItem.titleView = coolTitle
This feels like a UIKit bug for the following reason: if the title of the pushed view controller is wider than the title of the main view controller, it seems to work. Only when the secondary VC's title is narrower does the main VC's title get truncated during the transition.
There are a lot of possible workarounds for this, but I would recommend explicitly setting the titleView property of each VC's navigation item to an appropriately configured UILabel with an attributed string. In this case, you'd leave the title property of the navigation items empty. You might want to then configure the backBarButtonItem of each navigation item with an appropriate title (since otherwise your back buttons will have the generic title "Back").
I have a support feature in my app that allows users to email for support regarding the app via email. The issue is that while the rest of my app have white navigation bar text, the text in the navigation bar within the email view controller seems to be stuck set to black.
I have this code in the a tableview view controller that segues to a different view controller and on that view controller is a button which launches the email view controller:
override func viewWillAppear(animated: Bool) {
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
self.navigationController?.navigationBar.titleTextAttributes = attributes
}
The view controller that is segued to from the table view has the correct text colour, however the email view controller is still black.
I have tried doing:
mc.navigationBar.tintColor = UIColor.whiteColor()
But this doesn't seem to work. Any ideas?
Code example below:
[[UINavigationBar appearance] setTitleTextAttributes:#{UITextAttributeTextColor : [UIColor whiteColor]},NSFontAttributeName: UIFont(name: "Avenir", size: 24)];