How do i change navigationBar font in Swift?

2019-01-31 12:13发布

How do I change the NavigationBar font in Swift?

This is what I have tried so far, but receiving an error (I have correctly implemented CaviarDreams to the project):

self.navigationController.navigationBar.titleTextAttributes = NSFontAttributeName[UIFont .fontWithName(CaviarDreams.ttf, size: 20)]

Error says: Use of unresolved identifier 'CaviarDreams

Sorry if the question is extremely bad.

8条回答
狗以群分
2楼-- · 2019-01-31 12:48

Using Swift, I added this to AppDelegate.swift in

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

        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
        ]

        return true
    }

Hope it helps!

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-31 12:49

Swift 4.2

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

  let font = UIFont(name: "FontName", size: 16) ?? UIFont.systemFont(ofSize: 16)
  UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font: font]
}
查看更多
登录 后发表回答