Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中更改titleTextAttribute_Ios_Iphone_Swift_Appdelegate - Fatal编程技术网

Ios 在swift中更改titleTextAttribute

Ios 在swift中更改titleTextAttribute,ios,iphone,swift,appdelegate,Ios,Iphone,Swift,Appdelegate,自从我每次更新xcode之后,我似乎都无法更改titleExtAttribute。现在,当我使用以下代码时,会出现以下错误: 找不到接受此提供参数的重载初始化 appDelegate中的代码: UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 17), NSForegroundColorAttributeName:UIColor.whiteCo

自从我每次更新xcode之后,我似乎都无法更改
titleExtAttribute
。现在,当我使用以下代码时,会出现以下错误:

找不到接受此提供参数的重载初始化

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)

最近发生了一个更改,
UIFont(名称:size:)
返回可选的
UIFont
实例。你需要把它们拆开才能让它工作。使用
是最简单的方法,但如果字体不在系统上,则会导致崩溃。尝试以下方法:

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)

对于Swift 3,您可以尝试以下方法:

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

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
斯威夫特5
内特!内特!内特!内特!更多信息:必须将[NSObject:AnyObject]更改为[String:AnyObject],为什么要使用所有这些值?例如,只需键入NSAttributedStringKey.foregroundColor,无需通过(rawValue:).AppDelegate.shared.navigationController!启动它!。navigationBar.barTintColor=UIColor.white AppDelegate.shared.navigationController!。navigationBar.titleTextAttributes=[NSAttributedStringKey.foregroundColor:UIColor.black]
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]