【问题标题】:Change Both Title Text Color and Font in all Navigation Bars更改所有导航栏中的标题文本颜色和字体
【发布时间】:2016-05-31 11:02:24
【问题描述】:

我正在使用 swift 2 和最新版本的 Xcode,我正在尝试更改每个导航栏中标题文本的字体大小、名称和颜色。我在didFinishLaunchingWithOptions 区域的AppDelegate 部分编写了以下代码,但它只更改颜色,不影响字体的大小或类型。当我摆脱颜色线时,标题会跟随NSFontAttributeName 线。有没有办法让两行代码都执行?我还没有找到任何可以让你在代码中同时更改的东西...

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.


        UIApplication.sharedApplication().statusBarStyle = .LightContent
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: (UIFont(name: "AvenirNext-DemiBold", size: 50.0))!]
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor(red: 244/255, green: 234/255, blue: 166/255, alpha: 1.0)]


        return true
    }

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar


    【解决方案1】:

    您的代码很愚蠢。您设置了titleTextAttributes,然后丢弃刚刚设置的值并替换另一个值!

    UINavigationBar.appearance().titleTextAttributes = 
        [NSFontAttributeName: (UIFont(name: "AvenirNext-DemiBold", size: 50.0))!]
    UINavigationBar.appearance().titleTextAttributes = 
        [NSForegroundColorAttributeName : UIColor(red: 244/255, green: 234/255, blue: 166/255, alpha: 1.0)]
    

    如果这不是您想要做的,请将 titleTextAttributes 设置为 one 值,即由 both 设置组成的字典:

    UINavigationBar.appearance().titleTextAttributes = [
        NSFontAttributeName: (UIFont(name: "AvenirNext-DemiBold", size: 50.0))!,
        NSForegroundColorAttributeName : UIColor(red: 244/255, green: 234/255, blue: 166/255, alpha: 1.0)
    ]
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2012-08-30
      • 2014-07-30
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多