UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
I have put the above code in AppDelegate file, still color of the title is Black.
You could try with this:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .purple
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().isTranslucent = false
}
Try this,
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
UINavigationBar.appearance().titleTextAttributes = textAttributes
Above Problem occurred when I have updated my Xcode to 11.4
suddenly title color changed to black previously it was white
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = hexStringToUIColor(hex: "#7DB52F")
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white
]
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.buttonAppearance = buttonAppearance
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UIBarButtonItem.appearance().tintColor = UIColor.white
} else {
UINavigationBar.appearance().barTintColor = hexStringToUIColor(hex: "#7DB52F")
UINavigationBar.appearance().titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white
]
UINavigationBar.appearance().tintColor = UIColor.white
UIBarButtonItem.appearance().tintColor = UIColor.white
}
Related
I want to create navigation bar like in contacts application on iPhone, seems like default solution but I can't find any code examples. What I want to do is to move my navigation bar from the left to center and change font size when user scrolls tab bar. Please see screenshots:
Large title and title of navigation bar
class BaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 15.0, *) {
let scrollAppearance = UINavigationBarAppearance()
scrollAppearance.shadowColor = .white
scrollAppearance.backgroundColor = .white
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
navigationBarAppearance.backgroundColor = .white
navigationBarAppearance.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back-arrow")
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = scrollAppearance
navigationBar.tintColor = .black
navigationBar.prefersLargeTitles = true
navigationBar.isTranslucent = false
navigationItem.largeTitleDisplayMode = .automatic
} else {
navigationBar.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBar.tintColor = .black
navigationBar.prefersLargeTitles = true
navigationBar.isTranslucent = false
navigationItem.largeTitleDisplayMode = .automatic
navigationBar.barTintColor = .white
}
}
}
this line is responsible for showing this large left title in navigation bar
navigationItem.largeTitleDisplayMode = .automatic
this is the font for largeTitle and small center title
navigationBarAppearance.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
how to use it
let vc = ViewController()
let embeddedNav = BaseNavigationController(rootViewController: vc)
present(embeddedNav,animate:true)
set the title in view did load method of vc
navigationItem.title = "Some title"
Solution:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureNavigationBar(largeTitleColor: .black, backgoundColor: .spWhite, tintColor: .white, title: "Your Title", preferredLargeTitle: true)
}
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}
}
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. 🙂
On ios13, with iphone x, the large title navigation does not cover the status bar however when scrolling and transitioning into the traditional nav bar, it works perfectly. This doesn't affect devices without the notch.
Large titles
Traditional navigation bar
It's all embedded within a navigation controller so i'm lost as to why this is happening. Cheers
The official way to customize the UINavigationBar, pre iOS 13, is this:
// text/button color
UINavigationBar.appearance().tintColor = .white
// background color
UINavigationBar.appearance().barTintColor = .purple
// required to disable blur effect & allow barTintColor to work
UINavigationBar.appearance().isTranslucent = false
iOS 13 has changed how navigation bars work, so you'll need to do things slightly differently to support both old & new:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .purple
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().isTranslucent = false
}
Use my extension iOS 13 Swift 5 tested
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}}
How to use:
configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)
Set ViewController-based status bar...... to NO in info.plist if you want light Content
If you don't want largeTitles set it to false
for tranlsucent change navBarAppearance.configureWithOpaqueBackground() in:
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true
in the call set background color to .clear
UPDATE:
If you want to start with navigation controller and large Titles at first controller, don't forget to set launch controller in Scene Delegate like this:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: YourFirstViewController())
window?.rootViewController = vC
hope this help :)
For full application navigation bar support please add this extension inside your code.
import UIKit
extension UIViewController {
open func showNavigationBar(_ large: Bool,
_ animated: Bool,
titleColor: UIColor,
barTintColor: UIColor,
fontSize: CGFloat) {
navigationController?.navigationBar.barTintColor = barTintColor
navigationController?.navigationBar.backgroundColor = barTintColor
navigationController?.navigationBar.isTranslucent = true
self.navigationController?.setNavigationBarHidden(false, animated: animated)
if large {
self.navigationController?.navigationBar.prefersLargeTitles = true
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = barTintColor
appearance.titleTextAttributes = [.foregroundColor: titleColor]
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.compactAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
} else {
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]
}
} else {
self.navigationController?.navigationBar.prefersLargeTitles = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: 20.0)!]
}
}
}
And Then call this method simply
self.showNavigationBar(true, true, titleColor: UIColor.blue, barTintColor: UIColor.red, fontSize: 32.0)
In Xcode 10 unselectedItemTintColor property working properly but after Xcode 11 with ios 13 UITabbar unselectedItemTintColor property not working.
override func viewDidLoad() {
super.viewDidLoad()
myTabbar.unselectedItemTintColor = .red
}
iOS 13 with Xcode 11
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
appearance.stackedLayoutAppearance.selected.iconColor = UIColor.red
myTabbar.standardAppearance = appearance
}
In Case of : iOS 15 with Xcode 13
if #available(iOS 15, *) {
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = .white
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: UIColor.red]
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.black]
tabBarAppearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
tabBarAppearance.stackedLayoutAppearance.selected.iconColor = UIColor.red
tabBarView.standardAppearance = tabBarAppearance
tabBarView.scrollEdgeAppearance = tabBarAppearance
}
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
}