27

Every since i updated xcode i cant seem to change the titleTextAttribute. Now when i use following code i get this error:

could not find an overload init that accepts this supplied arguments

Code in appDelegate:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 17), NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Ubuntu-Light", size: 15), NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
cnbandicoot
  • 372
  • 3
  • 13
Peter Pik
  • 11,023
  • 19
  • 84
  • 142

7 Answers7

43

There was a recent change so that UIFont(name:size:) returns an optional UIFont instance. You'll need to unwrap them to get it to work. Using ! is the easiest way, but will get you a crash if the font isn't on the system. Try something like:

let navbarFont = UIFont(name: "Ubuntu", size: 17) ?? UIFont.systemFontOfSize(17)
let barbuttonFont = UIFont(name: "Ubuntu-Light", size: 15) ?? UIFont.systemFontOfSize(15)

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: navbarFont, NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: barbuttonFont, NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
12

Swift 4:

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
Steve Moser
  • 7,647
  • 5
  • 55
  • 94
  • 1
    AppDelegate.shared.navigationController!.navigationBar.barTintColor = UIColor.white AppDelegate.shared.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] – Jimmy_m Jun 27 '18 at 16:49
3

Swift 5

navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]
Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
2

For Swift 3 you could try the following:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
illright
  • 3,991
  • 2
  • 29
  • 54
0
    if let navFont = UIFont(name: "HelveticaNeue-Bold", size: 30.0) {
        let navBarAttributesDictionary: [NSObject: AnyObject]? = [
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSFontAttributeName: navFont
        ]
        navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary
    }
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36
0

Swift 4:

if let navFont = UIFont(name: "Futura", size: 18) {
   let navBarAttributesDictionary: [NSAttributedStringKey: Any] = [
   NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor(netHex: Colors.BabyBlue.rawValue),
   NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): navFont ]
   UINavigationBar.appearance().titleTextAttributes = navBarAttributesDictionary
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
  • Why are you using all those rawValues? Just type NSAttributedStringKey.foregroundColor for example, no need to initiate it through (rawValue:). – Andreas Astlind Mar 14 '18 at 09:37
0

Setting Title Attributes and NavigationBar background colour may not work for iOS 15 and higher versions. Please find the following extension to achieve it.

extension UINavigationBar {
func setDefaultTint(_ color: UIColor = .black, titleColor: UIColor = .white) {
    if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithDefaultBackground()
        appearance.backgroundColor = color
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor]
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    } else {
        barTintColor = color
        titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor]
    }
}

Note: call code in #available(iOS 15, *) in didFinishLaunchingWithOptions from AppDelegate if you want to keep navigation bar configuration throughout the application. In-case of different colours for different screens? call the function of setDefaultTint(_) before creating navigation controller.

Rajasekhar Pasupuleti
  • 1,598
  • 15
  • 22