2014-11-11 11 views
14

Od czasu aktualizacji xcode nie mogę zmienić nazwy titleTextAttribute. Teraz, gdy używam następujący kod otrzymuję ten błąd:Zmiana titleTextAttribute w swift

could not find an overload init that accepts this supplied arguments

kod w 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) 

Odpowiedz

32

Było tak, że ostatnia zmiana UIFont(name:size:) zwraca opcjonalnego instancji UIFont. Musisz je rozpakować, aby działał. Korzystanie z ! jest najprostszym sposobem, ale spowoduje awarię, jeśli czcionka nie znajduje się w systemie. Spróbuj coś takiego:

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

Więcej informacji: 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

zmień [NSObject: AnyObject] na [String: AnyObject] – FiddleMeRagged

+0

To nie działa w Swift4 – Satyam

2

Dla Swift 3 można spróbować wykonać następujące czynności:

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 
}