![](/img/trans.png)
[英]How can I change images for UIBarButtonItems of UINavigationController in QLPreviewController
[英]How can I change the default font for UIBarButtonItems?
我想更改所有 UIBarButtonItems 的默认字体。 我在我的应用程序的根视图 controller 中有以下代码:
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
UINavigationBar.appearance().titleTextAttributes = attributes
UINavigationBarAppearance().buttonAppearance.normal.titleTextAttributes = attributes
UIBarButtonItemAppearance().normal.titleTextAttributes = attributes
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "foo", style: .plain, target: nil, action: nil)
尽管外观发生了变化,但该条形按钮项的字体仍然是默认字体。 如何设置默认字体? 我知道我可以为每个单独的条形按钮项目设置字体,但我正在寻找一种方法来广泛地改变它。
iOS 13 前
class AppDelegate : NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if #available(iOS 13, *) {
}else{
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
UINavigationBar.appearance().titleTextAttributes = attributes
}
return true
}
}
iOS 13
class MyNavigationController : UINavigationController {
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
if #available(iOS 13, *) {
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
let appearance = UINavigationBarAppearance()
appearance.buttonAppearance.normal.titleTextAttributes = attributes
appearance.titleTextAttributes = attributes
self.navigationBar.standardAppearance = appearance
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您可以使用 UINavigationBarAppearance 自定义所有导航栏,但您需要在导航栏的外观代理上设置 standardAppearance - 调用如下:
UINavigationBarAppearance().buttonAppearance.normal.titleTextAttributes = attributes
UIBarButtonItemAppearance().normal.titleTextAttributes = attributes
孤立地什么也不做,因为他们创造了一个新的外观,设置一个值,然后把它扔掉。 而是这样做:
let appearance = UINavigationBarAppearance()
appearance.buttonAppearance.normal.titleTextAttributes = attributes
UINavigationBar.appearance().standardAppearance = appearance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.