Swift Custom NavBar Back Button Image and Text

后端 未结 12 1291
谎友^
谎友^ 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条回答
  • 2021-01-30 10:41

    I have tried all the above and all make the custom image without changing the text The only one worked for me is from this answer

    let backBTN = UIBarButtonItem(image: UIImage(named: "Back"), 
                                  style: .plain, 
                                  target: navigationController, 
                                  action: #selector(UINavigationController.popViewController(animated:)))
    navigationItem.leftBarButtonItem = backBTN
    navigationController?.interactivePopGestureRecognizer?.delegate = self
    
    0 讨论(0)
  • 2021-01-30 10:43

    Swift 4.2 Add this functions ViewController

    func addNavigationBarButton(imageName:String,direction:direction){
        var image = UIImage(named: imageName)
        image = image?.withRenderingMode(.alwaysOriginal)
        switch direction {
        case .left:
           self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
        case .right:
           self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
        }
    }
    
    @objc func goBack() {
        navigationController?.popViewController(animated: true)
    }
    
    enum direction {
        case right
        case left
    }
    

    Using you should use here

    viewDidLoad()

    addNavigationBarButton(imageName: "ic_back", direction:.left)
    
    0 讨论(0)
  • 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]
    }
    
    0 讨论(0)

  • For setting custom back bar button and remove text from back bar button, FROM STORYBOARD only, without any coding.

    0 讨论(0)
  • 2021-01-30 10:51

    swift 3

        extension UIViewController {
    
            func setBackButton(){
                let yourBackImage = UIImage(named: "backbutton")
                navigationController?.navigationBar.backIndicatorImage = yourBackImage
                navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
            }
    
        }
    
    0 讨论(0)
  • 2021-01-30 10:53

    Just in case someone need to change all Back buttons color or font with Swift5. UIBarButtonItem.appearance().tintColor = .red

    Add this to AppDelegate.swift file.

    import UIKit
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // Override point for customization after application launch.        
        UIBarButtonItem.appearance().tintColor = .white
        UIBarButtonItem.appearance().setTitleTextAttributes([
            NSAttributedString.Key.foregroundColor: .red,
            NSAttributedString.Key.font: UIFont(name: "font_name", size: 14)!
        ], for: .normal)
    
        return true
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题