![](/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.