ios 13上的导航栏标题字体问题
我使用的是Xcode 11.4和iOS 13.4。 我已使用UINavigatinBar.appearance设置了导航栏标题自定义字体 它工作正常,但在iOS 13+上,当我尝试推送到另一个VC,然后返回到父VC时,父VC标题字体突然被设置为默认字体,一秒钟后又变回自定义字体 以下是问题的gif:ios 13上的导航栏标题字体问题,ios,swift,xcode,xcode11.4,Ios,Swift,Xcode,Xcode11.4,我使用的是Xcode 11.4和iOS 13.4。 我已使用UINavigatinBar.appearance设置了导航栏标题自定义字体 它工作正常,但在iOS 13+上,当我尝试推送到另一个VC,然后返回到父VC时,父VC标题字体突然被设置为默认字体,一秒钟后又变回自定义字体 以下是问题的gif: 在这里,在ViewDidDisplay中管理它: let lblTitle = UILabel() let titleAttribute: [NSAttributedString.Key: Any
在这里,在ViewDidDisplay中管理它:
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
在这里,在ViewDidDisplay中管理它:
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
iOS 13.+采用UINavigationBarAppearance方法定制导航栏标题和导航栏按钮
检查此代码,可能会对您有所帮助
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)
}
iOS 13.+采用UINavigationBarAppearance方法定制导航栏标题和导航栏按钮
检查此代码,可能会对您有所帮助
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)
}
谢谢,它工作得很好。顺便问一下,你知道为什么另一种方法突然不起作用吗?这真的很奇怪。这是苹果的一个缺陷。确保在mac上添加feedback reporter,以便将来的xcode版本能够正确编译此文件,因为它工作得非常完美。顺便问一下,你知道为什么另一种方法突然不起作用吗?这真的很奇怪。这是苹果的一个缺陷。确保在mac上添加反馈报告器,以便将来的xcode版本能够正确编译此代码。我使用的是iOS 14和xcode 12,上面的代码根本没有生效。这很奇怪,我已经在UINavigationController中定义了上述内容extension@NoorxCode12和iOS面临同样的问题14@pawan_kumar导航栏背景没有为您更改,或者栏按钮属性没有更改?还是两者都有?@Noor我只检查了导航标题。我用的是UILabel。奇怪的是,我用的是iOS 14和Xcode 12,上面的代码根本就不起作用。奇怪的是,我在UINavigationController中定义了上面的代码extension@NoorxCode12和iOS面临同样的问题14@pawan_kumar导航栏背景没有为您更改,或者栏按钮属性没有更改?还是两者都有?@Noor我只检查了导航标题。我使用了UILabel。