2014-11-11 8 views
14

Ogni volta che ho aggiornato xcode non riesco a cambiare il titleTextAttribute. Ora, quando io uso seguente codice ottengo questo errore:Modifica titleTextAttribute in swift

could not find an overload init that accepts this supplied arguments

Codice 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) 

risposta

32

ci fu un cambiamento recente in modo che UIFont(name:size:) restituisce un UIFont un'istanza opzionale. Avrai bisogno di scartarli per farlo funzionare. L'utilizzo di ! è il modo più semplice, ma ti causerà un arresto anomalo se il carattere non si trova nel sistema. Provare qualcosa di simile:

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

Ulteriori informazioni: 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

dovuto a cambia [NSObject: AnyObject] in [String: AnyObject] – FiddleMeRagged

+0

Questo non funziona in Swift4 – Satyam

2

Per Swift 3 si potrebbe provare la seguente:

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 
}