Remove text from Back button keeping the icon

前端 未结 29 2423
栀梦
栀梦 2020-11-28 23:26

I want to remove the text from the back button, but I want to keep the icon. I have tried

let backButton = UIBarButtonItem(title: \"\", style: UIBarButtonIt         


        
相关标签:
29条回答
  • 2020-11-28 23:52

    In my case, for custom icon and title this did the trick (Swift 4)

        let imgBack = UIImage(named: "ic_back")
    
        navigationController?.navigationBar.backIndicatorImage = imgBack
        navigationController?.navigationBar.backIndicatorTransitionMaskImage = imgBack
    
        navigationItem.leftItemsSupplementBackButton = true
        navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
    
    0 讨论(0)
  • 2020-11-28 23:52

    Put this code in each VC which pushes another one

    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    
    0 讨论(0)
  • 2020-11-28 23:52

    To remove from all viewcontrollers in a navigation controller stack:

    subclass UINavigationController and add this:

    override func show(_ vc: UIViewController, sender: Any?) {
        setEmptyBackButton(vc)
        super.show(vc, sender: sender)
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        setEmptyBackButton(viewController)
        super.pushViewController(viewController, animated: animated)
    }
    
    func setEmptyBackButton(_ viewController: UIViewController) {
        viewController.navigationItem.backBarButtonItem =
            UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }
    
    0 讨论(0)
  • 2020-11-28 23:52

    The easy programmatic way, without unwanted side-effects, is initializing the navigationItem.backBarButtonItem with an empty item in the awakeFromNib method of the source controller (the one you are navigating from):

    override func awakeFromNib() {
        super.awakeFromNib()
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }
    

    Note: If you initialized the back button later, like in the viewDidLoad() method, than you would lose swipe-back functionality (swiping from the left edge to the right takes you one step back in the navigation stack).

    Then, if you want different back button texts for different destination controllers and if you're using segues, you can set the title in the prepare(for segue:, sender:) method, like this:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let item = navigationItem.backBarButtonItem {
            switch segue.identifier {
            case "SceneOne": item.title = "Back"; break
            case "SceneTwo": item.title = "Home"; break
            case "SceneThree": item.title = nil; break // Use this scene's title
            default: item.title = "" // No text
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 23:54

    You should select the navigation bar of the controller FROM which back button will point to and type " " in the Back Button field.

    e.g if you are pushing A controller to B controller, put whitespace in A controller navigation bar.

    0 讨论(0)
  • 2020-11-28 23:54

    This will solve your issue:

        import UIKit
    
        extension UINavigationController{
    
        func customizeBackArrow(){
            let yourBackImage = UIImage(named: "icon_back_arrow")
            self.navigationBar.backIndicatorImage = yourBackImage
            self.navigationBar.tintColor = Common.offBlackColor
            self.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
            navigationItem.leftItemsSupplementBackButton = true
            self.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", 
               style: .plain, target: self, action: nil)
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题