簡體   English   中英

如何更改導航欄后退按鈕的字體?

[英]How can I change the font of the back button for my navigation bar?

如何更改導航欄的后退按鈕的字體。

后退按鈕是“后退”或上一個視圖控制器的標題。

我認為這個viewDidLoad會起作用:

navigationController?.navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FONTNAME", size: 20)!], forState: UIControlState.Normal)

但是leftBarButton? 可選返回nil

如果您需要在整個應用程序中完全更改字體樣式(也就是每個導航按鈕),首選方法是使用UIBarButtonItem.appearance()代理。

示例代碼片段如下所示:

斯威夫特 3.0+

//make sure font name u use below *actually* exists in the system !
//if it doesn't app will crash because we're force unwrapping an optional (see below) !!!
let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], for: .normal)

將此代碼片段放在AppDelegate.swift文件開頭的某個位置是明智的,因為每次應用程序啟動時都必須進行字體樣式化。 此外,您可以安全地將此代碼放在進行 UI 樣式化和自定義的任何其他位置(例如Presenter類)。 執行此代碼后,您的所有 BarButton 都將在此后進行自定義。


但是作為一個真正的 Swift 🤓,你應該首先有選擇地解開你的字體,如果在字體加載過程中找不到它或任何其他可能的問題,最終會回退到系統字體。

var textAttributes: [String:Any]

//custom font setup
let fontColor = UIColor.purple
let barItemCustomFont = UIFont(name: "👑", size: 14)  //note we're not forcing anything here

//can we use our custom font 🤔
if let customFont = barItemCustomFont {
    //hooray we can use our font 💪
    textAttributes = [NSForegroundColorAttributeName: fontColor, NSFontAttributeName: customFont]
} else {
    //👎 not found -> omit setting font name and proceed with system font
    textAttributes = [NSForegroundColorAttributeName: fontColor]
}

//finally
UIBarButtonItem.appearance().setTitleTextAttributes(textAttributes, for: .normal)

真實世界的例子

在真正的應用程序中,您通常需要自定義UINavigationBarUIBarButton字體樣式(除了其他參數)以使它們在視覺上保持一致。 這是您可以使用的方便的stylizeNavigationFontOfMyAmazingApp🎨功能:

    func stylizeNavigationFontOfMyAmazingApp🎨() {
        //custom font
        let customFont = UIFont(name: "someFancyFont", size: 16)!  //note we're force unwrapping here

        //navigation bar coloring:
        UINavigationBar.appearance().tintColor = UIColor.white
        UINavigationBar.appearance().barTintColor = UIColor.blue

        //unique text style:
        var fontAttributes: [String: Any]
        fontAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: customFont]

        //navigation bar & navigation buttons font style:
        UINavigationBar.appearance().titleTextAttributes = fontAttributes
        UIBarButtonItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)

   }

使用appearance()代理的優點

  • 兩個 liner 代碼片段,用於對您的應用程序/項目中的每個UIBarButtonItem進行樣式化。
  • 您可以混合使用多個類的appearance()代理以獲得真正獨特的視覺風格
  • 如果您需要進一步的自定義控件,您可以在UIBarButtonItem特定實例上進一步重新自定義每個按鈕(例如放置自定義視圖、按鈕、圖像等)。

參考

關於appearance協議的Apple 文檔

剛剛測試了您的代碼,似乎該行返回nil的原因實際上是因為name: "FONTNAME"返回 nil。 因此,如果將該name屬性設置為有效的字體名稱,則代碼應該可以正常運行而不會出錯——即使navigationController?.navigationItem.leftBarButtonItem顯式設置為 nil。

但無論如何,正如我通過測試所看到的,這條線不會給你明顯想要的結果。 領先的navigationController optional 不應該在那里,因為它訪問您的UINavigationController而不是當前視圖。 只需使用UIViewController自己的navigationItem屬性直接訪問其leftBarButtonItem ,例如:

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)

編輯:根據您在此答案下發布的評論,似乎您實際上並不想設置leftBarButtonItem而是設置backBarButtonItem因為除了返回上一個視圖控制器之外,您不想實現自定義操作。

因此,在之前的視圖控制器(即您想要顯示自定義后退按鈕項目之前的視圖)中,您可以設置自定義后退按鈕而無需執行如下操作:

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)
navigationItem.backBarButtonItem = backButton

如果您想將此更改應用於所有應用程序,您可以使用以下代碼:

斯威夫特 4

我在application函數中將這些行添加到AppDelegate.swift中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.font: UIFont(name: "IranSansMobile", size: 20)!
        ]

        UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)

  return true
}

斯威夫特 3

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

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

    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)


    return true
}

對於 iOS 13、Swift 5.1:

let fontAttr = [NSAttributedString.Key.font: \*your font*\]

let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.normal.titleTextAttributes = fontAttr

let navbarAppearance = UINavigationBarAppearance()
navbarAppearance.buttonAppearance = buttonAppearance
UINavigationBar.appearance().standardAppearance = navbarAppearance

當您確定已經設置了UIBarButtonItem您可以執行以下操作:

self.navigationItem.leftBarButtonItem!.title = "myTitle"

要更改例如顏色,您可以執行以下操作:

self.navigationItem.leftBarButtonItem!.tintColor = UIColor.redColor()

當您尚未在故事板中設置它時,您可以執行以下操作:

self.navigationItem.leftBarButtonItem = UIBarButtonItem()

要更改字體,請執行以下操作:

self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FONTNAME", size: 20)!], forState: .Normal)

也許我應該提一下,你不能使用 self. navigationController因為在設置 navigationController 的按鈕時,它們不會顯示在屏幕上,因此屏幕上並在故事板中設置的按鈕必須是self.navigationItem ...

斯威夫特 3

    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Open Sans", size: 15)!,NSForegroundColorAttributeName: UIColor.white]
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Open Sans", size: 15)!], for: UIControlState.normal)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM