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

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

Related

Customizing UINavigationBar not working because of weird UINavigationBar view hierarchy

My app has many ViewControllers that are pushed on navigation stack.
I have configured UINavigationBar appearance globally in AppDelegate as below.
let appearance = UINavigationBar.appearance()
appearance.barTintColor = myColor
appearance.tintColor = .white
appearance.isTranslucent = false
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: myFont, size: mySize)]
appearance.titleTextAttributes = textAttributes as [NSAttributedString.Key : Any]
All ViewControllers work as expected except only one ViewController.
Below is one of the VCs that works as expected. It shows color and font that I want.
And below is the ViewController that shows different look unlike others.
I can't understand the reason why only one navigationBar on this VC shows different appearance.
So I've done debugging view hierarchy.
Below is the view hierarchy of VCs that works as expected.
And below is the view hierarchy of VC that shows weird look.
As seen on the picture, the problematic NavigationBar has two more layers, UIVisualEffectView and UIVisualEffectBackdropView.
I am an experienced iOS developer and have no idea why this happens.
I carefully checked all the setting related to NavigationBar on the IB but found no difference from others.
I even removed the ViewController, embedding NavigationController completely and rebuilt them from scratch without luck.
Please somebody explain me why only this NavigationBar has different structure.
I'm working on iOS 13.3 & Xcode 11.3.1
Working here on iOS15, I tried a lot of things to customize the nav bar appearance to achieve a simple opaque color, nothing worked except this:
In viewDidLoad
if (#available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.backgroundColor = [UIColor orangeColor];
self.navigationController.navigationBar.standardAppearance = appearance;
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
}
Swift version:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .orange
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
Finally if you only want to apply this on the current controller, and restore the default nav bar just do the same but with a fresh UINavigationBarAppearance() on viewWillDisappear.

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

Swift, Styling two navigation bar with code

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.

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