2014-11-11 8 views
14

Jedes seit ich aktualisiert xcode ich kann nicht scheinen, die titleTextAttribute zu ändern. Nun, wenn ich folgenden Code verwende ich bekomme diese Fehlermeldung:Ändern von titleTextAttribute in swift

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) 

Antwort

32

Es gibt eine aktuelle Änderung war so, dass UIFont(name:size:) eine optionale UIFont Instanz zurückgibt. Sie müssen sie auspacken, um es zur Arbeit zu bringen. Die Verwendung von ! ist der einfachste Weg, aber Sie werden einen Absturz bekommen, wenn die Schriftart nicht auf dem System ist. Versuchen Sie so etwas wie:

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) 
+1

Nate! Nate! Nate! Nate! – rob5408

+0

Weitere Informationen: http://stackoverflow.com/questions/27583346/how-can-i-change-the-font-of-the-back-button-for-my-navigation-bar/28347428#28347428 – Vexy

1
if let navFont = UIFont(name: "HelveticaNeue-Bold", size: 30.0) { 
     let navBarAttributesDictionary: [NSObject: AnyObject]? = [ 
      NSForegroundColorAttributeName: UIColor.blackColor(), 
      NSFontAttributeName: navFont 
     ] 
     navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary 
    } 
+2

Hatte zu Ändern Sie [NSObject: AnyObject] in [String: AnyObject] – FiddleMeRagged

+0

Dies funktioniert nicht in Swift4 – Satyam

2

Für Swift 3 Sie folgendes versuchen könnte:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
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 
} 
Verwandte Themen