最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift 设置NavigationBar的title字体颜色

Swift crifan 3508浏览 0评论
之前已经在didFinishLaunchingWithOptions中:
let ColorNavigationBarImageAndText:UIColor = UIColor.whiteColor()

       //set navi bar text and image color
        UINavigationBar.appearance().tintColor = ColorNavigationBarImageAndText
        //set navi bar background color
        UINavigationBar.appearance().barTintColor = ColorMainApp
        //UINavigationBar.appearance().barTintColor = UIColor.redColor()
        //set navi bar not translucent -> makesure background color precise/correct
        UINavigationBar.appearance().translucent = false

了,之前的导航栏的颜色也是正常的,是白色的。

不过后来发现,此处的字体颜色是自己主动设置的:

        currentTeamLabel.textColor = UIColor.whiteColor()

否则不设置的话,也是黑色的。

但是设置了UINavigationBar的tintColor,好像没生效啊。。

而进入子页面后,title的颜色是黑色的:

current navi title text color black

想要设置为白色

自己设置了:
//        print("UIBarButtonItem.appearance().tintColor=\(UIBarButtonItem.appearance().tintColor)")
//        UIBarButtonItem.appearance().tintColor = ColorNavigationBarImageAndText

没用。

[解决过程]

1.搜:swift UINavigationBar title text color

参考:

uinavigationbar – iOS 8: NavigationBar bar, tint, and title text color (swift) – Stack Overflow
UINavigationBar Text Color in Swift – Stack Overflow
ios – Text Color in Navigation Bar Swift – Stack Overflow
去设置:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : ColorNavigationBarImageAndText]
生效了:
字体为白色了:
set navi title text color white ok
然后再加上字体:
let FontNavigationBarText:UIFont = UIFont.systemFontOfSize(18)

        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: FontNavigationBarText,
            NSForegroundColorAttributeName : ColorNavigationBarImageAndText]
注:对应各种参数,详见:

转载请注明:在路上 » [已解决]swift 设置NavigationBar的title字体颜色

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.165 seconds, using 22.09MB memory