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
Screenshot for Avplayer and Video
Keyboard and Cursor Show, But I Can't Type Inside Uitextfields and Uitextviews
How to Create a Hex Color String Uicolor Initializer in Swift
Convert Nsdata to Sockaddr Struct in Swift
Color All Occurrences of String in Swift
Capture Redirect Url in Wkwebview in iOS
403 Error - Thats an Error. Error: Disallowed_Useragent
Alternative iOS Layouts for Portrait and Landscape Using Just One .Xib File
Disabling Automatic Scrolling of Uitableview When Editing Uitextfield Inside Uitableviewcell
Custom Font Not Working Programmatically in Swift
How to Round Corners of Uiimage with Hexagon Mask
Pixel Array to Uiimage in Swift
How to Add the Wechat API to a Swift Project
How to Render a Whole Uitableview as an Uiimage in iOS
iOS 13.1 Crash in Avaudio Player
Auto-Layout: What Creates Constraints Named Uiview-Encapsulated-Layout-Width & Height