Is it possible to disable the back navigation menu in iOS 14+?
It can be done by subclassing UIBarButtonItem. Setting the menu to nil on a UIBarButtonItem doesn't work, but you can override the menu property and prevent setting it in the first place.
class BackBarButtonItem: UIBarButtonItem {
@available(iOS 14.0, *)
override var menu: UIMenu? {
set {
// Don't set the menu here
// super.menu = menu
}
get {
return super.menu
}
}
}
Then you can configure the back button in your view controller the way you like, but using BackBarButtonItem instead of UIBarButtonItem.
let backButton = BackBarButtonItem(title: "BACK", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backButton
This is preferred because you set the backBarButtonItem only once in your view controller's navigation item, and then whatever view controller it will be pushing, the pushed controller will show the back button automatically on the nav bar. If using leftBarButtonItem instead of backBarButtonItem, you will have to set it on every view controller that will be pushed.
Edit:
The back navigation menu that appears on long press is a property of UIBarButtonItem. The back button of a view controller can be customized by setting the navigationItem.backBarButtonItem property and by doing so we can control the menu. The only problem with this approach that I see is losing the localization (translation) of the "Back" string which the system button has.
If you want the disabled menu to be the default behaviour you can implement this in one place, in a UINavigationController subclass conforming to UINavigationControllerDelegate:
class NavigationController: UINavigationController, UINavigationControllerDelegate {
init() {
super.init(rootViewController: ViewController())
delegate = self
}
func navigationController(_ navigationController: UINavigationController,
willShow viewController: UIViewController, animated: Bool) {
let backButton = BackBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
viewController.navigationItem.backBarButtonItem = backButton
}
}
Disable Long Press back Button (callout menu)
It can be done by subclassing UIBarButtonItem. Setting the menu to nil on a UIBarButtonItem doesn't work, but you can override the menu property and prevent setting it in the first place.
class BackBarButtonItem: UIBarButtonItem {
@available(iOS 14.0, *)
override var menu: UIMenu? {
set {
/* Don't set the menu here */
/* super.menu = menu */
}
get {
return super.menu
}
}
}
Then you can configure the back button in your view controller the way you like, but using BackBarButtonItem instead of UIBarButtonItem:
let backButton = BackBarButtonItem(title: "BACK", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backButton
This is the preferred way because you set the backBarButtonItem only once in your view controller's navigation item, and then whatever view controller it will be pushing, the pushed controller will show the back button automatically on the nav bar. If using leftBarButtonItem instead of backBarButtonItem, you will have to set it on every view controller that will be pushed.
UINavigationBar Hide back Button Text
In the interface builder, you can select the navigation item of the previous controller and change the Back Button
string to what you'd like the back button to appear as. If you want it blank, for example, just put a space.
You can also change it with this line of code:
[self.navigationItem.backBarButtonItem setTitle:@"Title here"];
Or in Swift:
self.navigationItem.backBarButtonItem?.title = ""
How to hide 'Back' button on navigation bar on iPhone?
Objective-C:
self.navigationItem.hidesBackButton = YES;
Swift:
navigationItem.hidesBackButton = true
How to remove all navigationbar back button title
If you want back arrow so following code put into AppDelegate
file into didFinishLaunchingWithOptions
method.
For Objective-C
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
For Swift
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
Another option give below.
In Objective C
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
In Swift
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
UPDATE :
let BarButtonItemAppearance = UIBarButtonItem.appearance()
let attributes: [NSAttributedStringKey: Any] = [
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
NSAttributedStringKey.foregroundColor: UIColor.clear]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
UPDATE SWIFT 4.1 :
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
Using Offset
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)
Swift - How to hide back button in navigation item?
According to the documentation for UINavigationItem
:
self.navigationItem.setHidesBackButton(true, animated: true)
Related Topics
Uitableviewcelldeleteconfirmationcontrol Issue
Want to Create a Cool Static UI But:"Static Table Views Are Only Valid..."
Change Width of a Uibarbuttonitem in a Uinavigationbar
Animating/Moving Views Under Usage of Autolayout
Nsinternalinconsistencyexception, Reason: Could Not Load Nib in Bundle
Union Uibezierpaths Rather Than Apend Path
Swiftui - Animations Triggered Inside a View That's in a List Doesn't Animate the List as Well
Swift - Compressing Video Files
App Transport Security Has Blocked a Cleartext Http Resource
Xcode 7 Supporting Watch Os1 and Os2
Split View Controller Must Be Root View Controller
Dynamically Increase Height of Uilabel & Tableview Cell
Uiwebview and Safari Comparison
Storing Uicolor Object in Core Data
Google Maps iOS Sdk: Custom Icons to Be Used as Markers
Changed +Load Method Order in Xcode 7
Hide Device Volume Hud View While Adjusitng Volume with Mpvolumeview Slider