Remove text from Back button keeping the icon

前端 未结 29 2337
栀梦
栀梦 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:12

    For Swift 4+ put these lines into AppDelegate at didFinishLaunchingWithOptions

    let BarButtonItemAppearance = UIBarButtonItem.appearance()
    
    BarButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)      
    BarButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)
    
    0 讨论(0)
  • 2020-11-29 00:13

    I know this already has an answer, but you can also do it in code (in case you're working with nibs)

    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    

    Add the above in the first view controller.

    Note that you need to do this for each view controller that is pushing. So if you have 3 view controllers, and you want to remove the back text from all of them, you'll have to add the line in view controller 1 and 2.

    0 讨论(0)
  • 2020-11-29 00:13

    If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.

    For Swift

    let BarButtonItemAppearance = UIBarButtonItem.appearance()
    BarButtonItemAppearance.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
    
    0 讨论(0)
  • 2020-11-29 00:13

    This is working for me

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    }
    
    0 讨论(0)
  • 2020-11-29 00:14

    One alternative to override all ViewControllers for me was to extend UINavigationController and set the backBarButtonItem of the topViewController.

    Swift 5 on Xcode 11.2.1:

    extension UINavigationController {
        override open func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
            let backButton = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
            self.topViewController?.navigationItem.backBarButtonItem = backButton
        }
    }
    
    0 讨论(0)
  • 2020-11-29 00:16

    in swift 4

    self.navigationController?.navigationBar.topItem?.title = ""
    
    0 讨论(0)
提交回复
热议问题