How to remove all navigationbar back button title

后端 未结 30 3293
后悔当初
后悔当初 2020-12-12 15:42

When I push a UIViewController, it has some title in back button at new UIViewController, if the title has a lot of text, It does not look good in

相关标签:
30条回答
  • 2020-12-12 15:57

    Just need go to your Parent ViewController from where your other ViewControllers are dependent.

    override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)}
    
    0 讨论(0)
  • 2020-12-12 15:57

    Simple Solution :

    While you are pushing 2nd controller from 1st controller, put self.navigationItem.title = "" in viewWillDisappear of 1st controller. It hides back button title from 2nd controller.

    Above statment hides 1st controllers title, hence when we came back we want title for 1st controller again. For that we have add title for 1st controller in viewWillAppear method of 1st controller.

    Refer following methods (of 1st controller)

        override func viewWillDisappear(_ animated: Bool) {
        self.navigationItem.title = ""
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationItem.title = "Title"
    }
    
    0 讨论(0)
  • 2020-12-12 15:58

    Swift 3:

    self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
    
    0 讨论(0)
  • 2020-12-12 15:59

    I usually add or change the back button in viewDidLoad of the UIViewController.

    Something like that should work:

    let leftButton = UIBarButtonItem(title: "Back", style:     UIBarButtonItemStyle.Plain, target: self, action: "closeView:")
    self.navigationItem.leftBarButtonItem = leftButton
    

    Don't forget to change and implement the function that it's called to close the view.

    Even easier, just change the title:

    self.navigationItem.leftBarButtonItem.title = "Back"
    
    0 讨论(0)
  • 2020-12-12 15:59

    You can add this extension to UIViewController And then call this function in every viewDidLoad() like : self.updateBackButton()

    extension UIViewController {
    func updateBackButton(){
        if self.navigationController != nil {
            self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
        }
    }}
    
    0 讨论(0)
  • 2020-12-12 16:00

    On iOS 14 is now present the backButtonDisplayMode property in UINavigationItem class. So, to remove back button title you can use

    navigationItem.backButtonDisplayMode = .minimal
    

    in the viewDidLoad func of the viewController where you want remove it.

    To remove it in all navigationBar I used the swizzling technique

    import UIKit
    
    private let swizzling: (UIViewController.Type, Selector, Selector) -> Void = { forClass, originalSelector, swizzledSelector in
        if let originalMethod = class_getInstanceMethod(forClass, originalSelector), let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
            let didAddMethod = class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
            if didAddMethod {
                class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
        }
    }
    
    extension UIViewController {
        
        static func swizzle() {
            let originalSelector1 = #selector(viewDidLoad)
            let swizzledSelector1 = #selector(swizzled_viewDidLoad)
            swizzling(UIViewController.self, originalSelector1, swizzledSelector1)
        }
        
        @objc open func swizzled_viewDidLoad() {
            if let _ = navigationController {
                if #available(iOS 14.0, *) {
                    navigationItem.backButtonDisplayMode = .minimal
                } else {
                    // Fallback on earlier versions
                    navigationItem.backButtonTitle = ""
                }
            }
            swizzled_viewDidLoad()
        }
    }
    

    And in application(_:didFinishLaunchingWithOptions:) call

    UIViewController.swizzle()
    
    0 讨论(0)
提交回复
热议问题