Navigation Bar title font problem on ios 13 - ios

Iā€™m using Xcode 11.4 and iOS 13.4.
I have set navigation bar title custom font using UINavigatinBar.appearance()
And it works correctly but on iOS 13+ when i try to push to another VC and then comeback to the parent VC, the parent VC title font suddenly has been set to default font and after a second it changes back to the custom font.
Below is a gif of the problem:
nav bar font problem

iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems
Check this code, might help you
let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
UINavigationBar.appearance().tintColor = UIColor.white // bar icons
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red // If you want different nav background color other than white
appearance.titleTextAttributes = titleFontAttrs
appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
UINavigationBar.appearance().isTranslucent = false
appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = .red // bar background
UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
UINavigationBar.appearance().isTranslucent = false
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
}

Here you go, manage it in viewDidAppear:
let lblTitle = UILabel()
let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
.foregroundColor: UIColor.black]
let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
lblTitle.attributedText = attributeString
lblTitle.sizeToFit()
navigationItem.titleView = lblTitle

Related

Changing UIActivityViewController share modal navigation bar to opaque on iOS15

I'm getting a transparent navigation bar in share modal controllers on iOS 15. But navbar appearance were properly set already and is not changed at any part of the app. This issue only happens from uiactivity share modals and not all .
Appearance setup
// AppDelegate.swift
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
appearance.shadowImage = nil
appearance.shadowColor = .clear
appearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
NSAttributedString.Key.foregroundColor: UIColor.Function.black
]
navBarAppearance.barTintColor = .white
navBarAppearance.standardAppearance = appearance
navBarAppearance.scrollEdgeAppearance = appearance
navBarAppearance.compactAppearance = appearance
} else {
navBarAppearance.isTranslucent = false
navBarAppearance.backgroundColor = .white
// Remove bottom line aka shadow
navBarAppearance.shadowImage = UIImage()
navBarAppearance.setBackgroundImage(UIImage(), for: .default)
navBarAppearance.tintColor = .clear
navBarAppearance.layer.shadowOpacity = 0
// Update bar title font
navBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
NSAttributedString.Key.foregroundColor: UIColor.Function.black
]
}
}
What I've tried and it does nothing.
func presentShareActivity(items: [Any]) {
//let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
let activityVC = UIActivityViewController(activityItems: ["test share copy"], applicationActivities: nil)
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
appearance.shadowImage = nil
appearance.shadowColor = .clear
appearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
NSAttributedString.Key.foregroundColor: UIColor.Function.black
]
activityVC.navigationItem.scrollEdgeAppearance = appearance
activityVC.navigationItem.standardAppearance = appearance
activityVC.navigationItem.compactAppearance = appearance
activityVC.navigationController?.navigationBar.isTranslucent = false
activityVC.navigationController?.navigationBar.scrollEdgeAppearance = appearance
activityVC.navigationController?.navigationBar.standardAppearance = appearance
activityVC.navigationController?.navigationBar.compactAppearance = appearance
}
present(activityVC, animated: true)
}
Screenshots
Navigation bar is transparent but turns opaque on scroll. I want this to be opaque
Interestingly the photo below uses the same code but has an opaque navbar
Try this:
activityVC.view.backgroundColor = UIColor.systemBackground
It worked for me. šŸ™‚

iPadOS 15 UITabBar title cut off

Since I upgraded my iPad operating system, the title of the UITabBar of my app is showing truncated, as shown in the screenshot.
I have tried some methods, but I have not found the correct solution.
Hope someone can help me.
And Here is code:
func setupTabBar() {
if #available(iOS 13, *) {
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.backgroundImage = UIImage(color: .white)
appearance.shadowImage = UIImage(color: .clear)
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
UITabBar.appearance().standardAppearance = appearance
} else {
tabBar.backgroundImage = UIImage(color: .white)
tabBar.shadowImage = UIImage(color: .clear)
}
if #available(iOS 15, *) {
UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
}
For some reason, it seems that setting the titleTextAttributes is what causes the problem to happen with inlineLayoutAppearance, and including the default paragraph style of NSParagraphStyle.default fixes it.
For your code, the following changes should fix it (as of iOS 15.0).
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray, .paragraphStyle: NSParagraphStyle.default]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red, .paragraphStyle: NSParagraphStyle.default]
For those that cannot have a default paragraph style, setting the attributes for most States got the OS to size the labels correctly for me.
Swift 5.0:
UITabBarItem.appearance()
.setTitleTextAttributes(
customAttributesWithCustomParagraphStyle,
for: [
.normal,
.highlighted,
.disabled,
.selected,
.focused,
.application,
]
)
Without setting it for at least .normal and .selected, the UITabBarItem title gets truncated when used with custom attributes.

Change title color of navigation bar in MFMailComposeViewController in iOS 12 not working [duplicate]

This question already has an answer here:
iOS 12.0 : Is there a way to set MFMailComposeViewController navigation bar title's text to white?
(1 answer)
Closed 1 year ago.
How can I change the title color of UINavigationBar in MFMailComposeViewController in iOS 12?
This is what I am doing:
import MessageUI
extension MFMailComposeViewController {
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
navigationBar.isOpaque = false
navigationBar.barTintColor = .white
navigationBar.tintColor = .white
navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
}
In iOS 10 works:
In iOS 11 works:
In iOS 12 is not working:
/// UINavigationBar
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.backgroundColor = .red
navBarAppearance.titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.systemFontSize
]
navBarAppearance.largeTitleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.smallSystemFontSize
]
let barButtonAppearance = UIBarButtonItem.appearance()
barButtonAppearance.setTitleTextAttributes([.font: UIFont.systemFontSize], for: .normal)
navBarAppearance.isTranslucent = false
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = navBarAppearance.backgroundColor
appearance.titleTextAttributes = navBarAppearance.titleTextAttributes ?? [:]
appearance.largeTitleTextAttributes = navBarAppearance.largeTitleTextAttributes ?? [:]
navBarAppearance.standardAppearance = appearance
navBarAppearance.scrollEdgeAppearance = appearance
}
I tried all the way to change the title color, however it doesn't work
Before presenting the mailcomopser controller
I changed the background color to white
and buttons color to black
Here is the code below:
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = UIColor.white
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .disabled)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
In your AppDelegate.swift file in the didFinishLaunchingWithOptions launchOptions block
TRY THIS:
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = .blue //your desired color
navigationBarAppearace.tintColor = .white //your button etc color
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //your Title Text color
titleView still works fine. Just create view with label in story board like this, with such font, as U need and set it as title view.
if let view = Bundle.main.loadNibNamed("WTitleView", owner: self, options: nil)?.first as? UIView {
navigationItem.titleView = view
}

Change iOS 11 large title color

I'm using the new enlarged navigation bar titles in iOS 11. But I can't seem to be able to change the textColor.
I tried doing:
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
This didn't do anything. Any ideas?
self.navigationController.navigationBar.largeTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
I think it's still a bug in Xcode 9 beta 6.
I found different "solutions" for it:
It's possible to change the color of the title if you put this in the AppDelegate:
if #available(iOS 11.0, *) {
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
}
Other way is to set the color in your Controller's viewDidLoad, but the secret to make it work is to set the font also:
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 31, weight: UIFont.Weight.bold) ]
}
Hope it helps you.
Regards!
iOS 11
Objective-C
if (#available(iOS 11.0, *)) {
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = true;
// Change Color
self.navigationController.navigationBar.largeTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
} else {
// Fallback on earlier versions
}
Swift 5.6.1
In my Swift 5.6.1 and iOS 15.6.1 the following code worked only.
Add the following codes in ViewDidLoad()
let appearance = UINavigationBarAppearance(idiom: .phone)
// Add the color you want in your title
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
// Add the color you want as your navigation bar color. Otherwise it shows White by default
appearance.backgroundColor = .purple
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
Swift 4.2
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
with named color
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(named: "Teal") ?? UIColor.black]
Swift up through Swift 3.2 (not Swift 4+)
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
let largeTitleTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.gray20, .font: UIFont.systemFont(ofSize: 24.0, weight: .bold)]
if #available(iOS 15, *) {
let navigationBar = navigationController.navigationBar
let appearance = navigationBar.standardAppearance
appearance.largeTitleTextAttributes = largeTitleTextAttributes
navigationBar.standardAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
} else {
navigationController.navigationBar.largeTitleTextAttributes = largeTitleTextAttributes
}

how to change navigationitem title color

I think all day to change the navigation Bar title color, but it doesn't work. this is my code:
var user: User? {
didSet {
navigationItem.title = user?.name
observeMessages()
}
}
I use didSet to show the title on the navigation title.
Add this in your code . .
let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4:
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4.2+:
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Keeping all the other attributes of the title:
If you just want change the color you could do like this:
if var textAttributes = navigationController?.navigationBar.titleTextAttributes {
textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red
navigationController?.navigationBar.titleTextAttributes = textAttributes
}
The title color of Navigation Bar can be changed in Storyboard.
Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu.
Solution for iOS 13
To customize the appearance of a navigation bar you need to use UINavigationBarAppearance:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.red]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
Swift 4
set this first
navigationController?.navigationBar.barStyle = .default
then one of those should work
navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
or
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
Swift 5
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
or
navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
For iOS 13 you have to change the color in the appearance property and for older iOS versions you can do it directly in the navigation bar property.
if #available(iOS 13.0, *) {
navigationController?.navigationBar.standardAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
} else {
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
in objetive c:
[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], NSForegroundColorAttributeName,
[UIFont fontWithName:#"ArialMT" size:16.0], NSFontAttributeName,nil]];
The didSet is called if your set the user, maybe you're setting the user's name variable and expecting the program to enter didSet.
try setting the user.
And if you want to change the color of the text when the navigation title is changed to the name of the user just call this code.
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.red]
var user: User? {
didSet {
navigationItem.title = user?.name
}
}
override func viewDidLoad() {
super.viewDidLoad()
let newUser = User()
newUser.name = "John Doe"
user = newUser
}
If you set your navigation bar's title to prefer large titles, like so:
navigationBar.prefersLargeTitles = true
then you need to use the largeTitleTextAttributes property and not the titleTextAttributes property. If you set your nav title to be a large title, the titleTextAttribute is not the correct property to use. Use the largeTitleTextAttributes property, like so:
navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
Swift 5/Xcode 11
Write this code as extension from UINavigationBar
extension UINavigationBar {
func customNavigationBar() {
// color for button images, indicators and etc.
self.tintColor = UIColor.Custom.mainAppColor
// color for background of navigation bar
// but if you use larget titles, then in viewDidLoad must write
// navigationController?.view.backgroundColor = // your color
self.barTintColor = .white
self.isTranslucent = false
// for larget titles
self.prefersLargeTitles = true
// color for large title label
self.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
// color for standard title label
self.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.someColor]
// remove bottom line/shadow
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
}
}
then in AppDelegate.swift call UINavigationBar.appearance().customNavigationBar() in didFinishLaunchingWithOptions function
you can just add this line of code in your AppDelegate in the func
didFinishLaunchingWithOptions
Code you will add to change title color:
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]
you can add this line as well if you wanna change the backButton color
UINavigationBar.appearance().tintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)
Finally you can add this line to change the background color:
UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)
Note: You can change the values dependent on the color you want
and have a happy coding day
Swift 4
create project and test this with ViewController easy to use
import UIKit
class ProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
}
func configureNavigationBar() {
navigationItem.title = "Profile"
let textChangeColor =[NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
navigationItem.rightBarButtonItem?.tintColor = .white
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_right").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.rightBarButtonItem?.tintColor = .white
}
}

Resources