2014-11-11 17 views
14

Mỗi khi tôi cập nhật xcode i cant dường như thay đổi titleTextAttribute. Bây giờ khi tôi sử dụng đoạn mã sau tôi nhận được lỗi này:Thay đổi titleTextAttribute trong swift

could not find an overload init that accepts this supplied arguments

Mã trong 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) 

Trả lời

32

Có một sự thay đổi gần đây để UIFont(name:size:) trả về một tùy chọn UIFont dụ. Bạn sẽ cần phải cởi mở chúng để làm cho nó hoạt động. Sử dụng ! là cách dễ nhất, nhưng sẽ khiến bạn gặp sự cố nếu phông chữ không có trên hệ thống. Hãy thử một cái gì đó như:

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

Thông tin khác: 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

Đã phải thay đổi [NSObject: AnyObject] thành [String: AnyObject] – FiddleMeRagged

+0

Điều này không hoạt động trong Swift4 – Satyam

2

Đối Swift 3 bạn có thể thử như sau:

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 
} 
Các vấn đề liên quan