我正在使用Xcode 11.4和iOS 13.4。 我已经使用UINavigatinBar.appearance()设置了导航栏标题自定义字体 它可以正常工作,但是在iOS 13+上,当我尝试推送到另一个VC然后又返回到父VC时,父VC标题字体突然被设置为默认字体,并在一秒钟后又更改回自定义字体。 / p>
以下是问题的gif图像:
答案 0 :(得分:3)
iOS 13. +具有UINavigationBarAppearance
方法来自定义NavigationBar-Title和NavigationBar-BarButtonItems
检查此代码,可能会对您有所帮助
let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
UINavigationBar.appearance().tintColor = UIColor.white // bar icons
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red // If you want different nav background color other than white
appearance.titleTextAttributes = titleFontAttrs
appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
UINavigationBar.appearance().isTranslucent = false
appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = .red // bar background
UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
UINavigationBar.appearance().isTranslucent = false
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
}
答案 1 :(得分:2)
在这里,您可以在viewDidAppear中对其进行管理:
let lblTitle = UILabel()
let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
.foregroundColor: UIColor.black]
let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
lblTitle.attributedText = attributeString
lblTitle.sizeToFit()
navigationItem.titleView = lblTitle