在Swift中更改titleTextAttribute

27

自从我更新了Xcode之后,似乎无法更改titleTextAttribute。现在当我使用以下代码时,会出现以下错误:

找不到接受此提供的参数的重载init

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)
7个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
43

最近有个改变,使得UIFont(name: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)

1
Nate!Nate!Nate!Nate! - rob5408
更多信息:https://dev59.com/AV4c5IYBdhLWcg3wvsfB#28347428 - Vexy

12

Swift 4:

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]

1
AppDelegate.shared.navigationController!.navigationBar.barTintColor = UIColor.white AppDelegate.shared.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] - Jimmy_m

3

Swift 5

navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]

2

对于Swift 3,您可以尝试以下操作:

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
}

你为什么要使用那么多的rawValues?例如,只需键入NSAttributedStringKey.foregroundColor,无需通过(rawValue:)进行初始化。 - Andreas Astlind

0

设置标题属性和NavigationBar背景颜色可能在iOS 15及更高版本中无法正常工作。 请使用以下扩展来实现它。

extension UINavigationBar {
func setDefaultTint(_ color: UIColor = .black, titleColor: UIColor = .white) {
    if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithDefaultBackground()
        appearance.backgroundColor = color
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor]
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    } else {
        barTintColor = color
        titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor]
    }
}

注意:如果您想在整个应用程序中保持导航栏配置,请在AppDelegate的didFinishLaunchingWithOptions中调用#available(iOS 15, *)中的代码。 如果不同的屏幕需要不同的颜色?在创建导航控制器之前调用setDefaultTint(_)函数。


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

2
不得不将 [NSObject:AnyObject] 更改为 [String: AnyObject]。 - FractalDoctor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,