Swift Custom NavBar Back Button Image and Text

后端 未结 12 1316
谎友^
谎友^ 2021-01-30 10:40

I need to customise the look of a back button in a Swift project.

Here\'s what I have:

Here\'s what I want:

I\'ve tried creating my own UIBarButtonItem

12条回答
  •  旧时难觅i
    2021-01-30 10:46

    For the back button image:

    • By this tutorial: (but didn't work for me)

      UINavigationBar.appearance().backIndicatorImage = UIImage(named: "imageName")
      
    • But this stack answer: (worked for me)

      var backButtonImage = UIImage(named: "back-button-image")
      backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 15, topCapHeight: 30)
      UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
      

    And for the font, assuming you want the font to match for the whole navigation bar:(currently in use)

    if let font = UIFont(name: "Avenir-Book", size: 22) {
      UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
    }
    

提交回复
热议问题