Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable back button in navigation bar

Is there any official way how to set UIBarButtonItem.enabled property? I tried to set a backButtonItem in previous controller. But enabled property is ignored.

More in this simple example project.

I don't want to some solution like "make your own leftBarButtonItem and set its alpha ..."

Edit: I don't want to hide it, only disable it with dimmed colour and disabled user interaction. It's exactly the same behaviour as for disabled leftBarButtonItem.

like image 351
Deny Avatar asked Aug 14 '15 12:08

Deny


People also ask

How do I hide the back button on my navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Full screen gestures”. The navigation bar will be hidden and you will be shown “Gesture demos” to explain how to operate the “Go back”, “Go to Home screen” and “Open Recents” functions.

How do I disable the Back button on my Iphone?

Yes, the back button can be disabled. Please navigate to Advanced Website Kiosk Settings–>Navigation–>Disable back button. Kindly enable this restriction to disallow the usage of the back button on the iOS device.

How do I hide the navigation bar back button in Swiftui?

The . navigationBarBackButtonHidden(true) will hide the back button.


2 Answers

As of today it is not possible to disable the back button using the enabled property. The backBarButtonItem property will be nil unless you create a custom item and even then it will ignore the enabled property. There are a couple (non-satisfactory) ways around this.

Hide the button

This is what Apple wants you to do given that they ignore the enabled property. It is as simple as

navigationItem.hidesBackButton = true   

and should be the preferred approach unless you have good reasons.

Disable and Tint the Navigation Bar

You can disable user interaction on the whole navigation bar and tint it to make the back button appear disabled.

navigationController?.navigationBar.isUserInteractionEnabled = false navigationController?.navigationBar.tintColor = UIColor.lightGray 

This does, unfortunately, affect other elements in the navigation bar as well so it might not be an option if, for instance, you have another bar button item on the right side.

Use a Custom Left Bar Button Item

The leftBarButtonItem does not ignore the enabled property so you could create a custom item and trigger the pop manually when it is activated.

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ThisClass.backButtonTapped)) ... navigationItem.leftBarButtonItem?.isEnabled = false  func backButtonTapped() {     self.navigationController?.popViewController(animated: true) } 

This will, however, not have the back bar button style with the leading triangular indicator.

like image 116
hennes Avatar answered Sep 21 '22 15:09

hennes


Add below code in your ViewController2.swift Class.

override func viewDidLoad() {         super.viewDidLoad()          navigationItem.hidesBackButton = true;     } 

It will hide your back button.

like image 36
BKjadav Avatar answered Sep 19 '22 15:09

BKjadav