Skip to content

Instantly share code, notes, and snippets.

@konnnn
Last active February 15, 2021 18:29
Show Gist options
  • Save konnnn/8e24111377a19d654db533e0f3daea68 to your computer and use it in GitHub Desktop.
Save konnnn/8e24111377a19d654db533e0f3daea68 to your computer and use it in GitHub Desktop.
Customize Navigation Bar Font and Color
// NavigationBar+Custom.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// navigation bar title
self.navigationItem.title = "Navigation Bar"
// configure navigation bar
navigationController?.configure(withFontSize: 24.0, andWeight: .black)
}
}
extension UINavigationController {
/// Customize Navigation Bar Font and Color
func configure(withFontSize size: CGFloat, andWeight weight: UIFont.Weight = .regular) {
// status bar color: black means the background is black and text is white
self.navigationBar.barStyle = .black
// background color
self.navigationBar.barTintColor = .black
// color left and right navigation bar items
self.navigationBar.tintColor = .white
// font size and weight
var customFont = UIFont()
//
if let descriptor = UIFont.systemFont(ofSize: size, weight: weight).fontDescriptor.withDesign(.rounded) {
// set rounded system font
customFont = UIFont(descriptor: descriptor, size: size)
} else {
// set default font design if rounded not available
customFont = UIFont.systemFont(ofSize: size, weight: weight)
}
// title attributes
self.navigationBar.titleTextAttributes = [
// title custom font size and weight
NSAttributedString.Key.font: customFont,
// title color
NSAttributedString.Key.foregroundColor: UIColor.white
]
}
}
@konnnn
Copy link
Author

konnnn commented Oct 18, 2020

3m3bVC0lkKA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment