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
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)}
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"
}
Swift 3:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
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"
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)
}
}}
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()