Swift - How to Hide Back Button in Navigation Item

Swift - How to hide back button in navigation item?

According to the documentation for UINavigationItem :

self.navigationItem.setHidesBackButton(true, animated: true)

Hide back button in navigation bar with hidesBackButton in Swift

Try adding this:

let backButton = UIBarButtonItem(title: "", style: .Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton

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 can I hide the back arrow in the navigation bar's back button?

If you do ,

navigationController?.navigationBar.backIndicatorImage = nil
navigationController?.navigationBar.backIndicatorTransitionMaskImage = nil

or

navigationController?.navigationBar.backIndicatorImage = UIImage(named: "")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "")

Then by default iOS use < as default image.

To remove this image you need to add blank transparent image. I used sample from here and google image reference is here

Now reduce the image pixel size to (1,1) and add in your assets and use below code.

navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Blank")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Blank")

Here Blank is the same image name in my asset.

Swift hide back button

First, Create BaseViewController in your project and set backButton hide code and add custom back button code in viewDidLoad.

After that, all the controller of your project should inherit from BaseViewController so new back button enables for all controller.

BaseViewContorller

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
self.setBackButton()// Set Custom back button
}

Set Custom BackButton Code

//Add Custom Back Button
fileprivate func setBackButton() {
let button = UIButton(type: UIButton.ButtonType.custom) as UIButton
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.imageView?.contentMode = .center
button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)

button.setImage(UIImage(named: "backButton.png"), for: .normal)
button.addTarget(self, action: #selector(btnBackActionHandler(_:)), for:.touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

}


@objc func btnBackActionHandler(_ sender : AnyObject) {
self.navigationController?.popViewController(animated: true)
}

Hide navigation bar but keep back button - SwiftUI

I seemed to have resolved my problem by following this and using @Environment

So instead of using a NavigationLink in my final tab like this:

ZStack(alignment: .topLeading) {
Text("Tab1View")
NavigationLink(destination: ProductList()){
Image(systemName: "chevron.backward")
}
}

I am now using a button that dismisses the view like this using @Environment:

struct Tab1View: View {

@Environment(\.presentationMode) var presentation

var product: ProductModel
var body: some View {
ZStack(alignment: .topLeading) {
Text("Tab1View")
Button(action: {
self.presentation.wrappedValue.dismiss()
}, label: {
Text("PressMe")
})
}
}
}


Doing this allows me to hide the NavigationBar in the TabView the same way:

.navigationBarTitle("")
.navigationBarHidden(true)


Related Topics



Leave a reply



Submit