Swift, Styling two navigation bar with code - ios

I'm developing an app on which requires two navigation bar at the top. I was planning on inputting custom fonts for both titles, and I did it by code in my View Controller's viewDidLoad like so,
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "SignPainter-HouseScript", size: 30)!, NSForegroundColorAttributeName:UIColor.whiteColor()]
Doing this, both my navigation bar's title changed, which is not what I intended it to do. How do I customize each navigation bar separately in one view controller, if possible?

If you use appearance proxy, it will apply global settings.
To get different style for two different object, you have change the same property for a specific object.
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "SignPainter-HouseScript", size: 30)!, NSForegroundColorAttributeName:UIColor.whiteColor()]
Wrire above code in viewDidLoad: of your first viewController.
It will change the titleTextAttributes of first navigation controller.
Similarly get object of other navigationController and change the same property.

you can set custom navigation title view like this:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.titleView = "a custom UIView"
}
so that every viewController have their own custom title view.

Related

Navigation Bar and Status Bar colors/ invisible iOS

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) }`

UINavigationController Title Not Using Correct Tint

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])

ios swift: lag when changing navigationbar title font across different VCs

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").

Back button text colour reverts to default

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 :)

Mail compose view controller text colour won't change

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)];

Resources