Remove text from Back button keeping the icon

前端 未结 29 2639
栀梦
栀梦 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-29 00:08

    For the case where we have not control at all of the previous view controller (i.e. if we are working in a framework), we can delete the title of the back button as follow:

    // For iOS 10
    navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = String()
    
    // For iOS 11
    navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = nil
    

    What it does is to navigate to the last item of the navigation's stack and delete its back title. Make sure to save the original one when our view controller will appear:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        originalBackButtonTitle = navigationController?.navigationBar.items?.last?.backBarButtonItem?.title
        // Delete title somewhere here...
    }
    

    and then reassign it, in order to not disrupt any part of the app:

    override func viewWillDisappear(_ animated: Bool) {
        navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = originalBackButtonTitle
        super.viewWillDisappear(animated)
    }
    

提交回复
热议问题