I've a NavigationViewController which segues to a TabbedBarController. I don't want to show the back button on the TabbedBarController.
I've tried both these code snippets in Swift , neither works,
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.hidesBackButton = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.hidesBackButton = true
}
Here's a snap shot,
Here's the tabbed bar view controller,
How can I hide the back button on the Tabbed bar view controller. How can I hide the back button on my tabbed bar controller?
There are quite many ways to do what you want, but what I'm 99.9% sure that would work is that you can add a barButton in your leftBarButtonItems to replace the default backButton of your navigationController:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.leftBarButtonItems = [UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)]
}
Also, when overriding any lifecycle methods of a controller, you might not want to miss anything from the parent class, so don't forget to call its super equivalent like super.viewWillAppear(animated)
I hope this helps!
EDIT: Two ways to do what you want in tabBarController and since I can already picture the flow of your project.
Put the code inside the viewWillAppear of your tabBarController. This means that you might need to subclass the UITabBarController. So it should be like this:
class MyTabBarController: UITabBarController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.leftBarButtonItems = [UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)]
}
}
You should present modally your tabBarController (this is more ideal).
Related
I was having this issue and I've tried a lot of solutions that was proposed by some kind people here in the following topic:
Swift - How to hide back button in navigation item
I created a ViewController class:
import SwiftUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationController?.navigationBar.isHidden = true
//self.navigationItem.backButtonTitle = "hohoho"
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true
//UINavigationBar.appearance().isHidden = true
//navigationItem.backBarButtonItem = UIBarButtonItem(title: "Home/Return or nohing", style: .bordered, target: nil, action: nil)
}
}
and AS you can see in the above code I tried every single way with no change - back button still appear - then I try to make simple change like change the text of the back button or the shape and also there is no result!!
Am I do something wrong :( Because I feel like the whole class is not active for my view
Do I need to create an object of ViewController or something like that? Because I just wrote the mentioned code about my view code.
MY GOAL: I just want to move from view to another with no back button if there is another way I wouldn't mind to do it.
PPLLLSSSS HELPP ME Guys I'm so tired, I'll work on another things until find a solution for that and I'm sure there is a lot of people who want a solution for that issue.
Once I find the solution I'll share it with you guys :) Best Wishes and Regards
You just have to add the below code in the ViewController where you want to hide the backbutton.
navigationItem.setHidesBackButton(true, animated: true)
Segue from viewController to viewController2 and name the segue testSegue. This should work.
I had the exact same problem and the only solution that worked was adding
.navigationBarBackButtonHidden(true) in my SwiftUI view
Issue: Upon displaying the second view, the Back button shows and then quickly disappears.
I'd like the Back button to persist on the second view.
Setup:
2 views.
The button that opens the second view is done via a "Show" segue.
SecondVC:
override func viewDidAppear(_ animated: Bool) {
let controller = TipJarViewController<TipJarOptions>()
self.present(controller, animated: false, completion: nil)
}
It seems to happen because of how I'm doing the viewDidAppear. It seems that I'm replacing the entire view with the self.present. I'm not sure what to search for or modify so it can still show the Back button.
Bonus question: Wondering if I'm placing this code in the wrong section. On the transition to the second view the screen is blank for a bit and then will show the view's contents. This doesn't seem like an optimal user experience. Open to any suggestions here.
After moving present to first VC via Frankenstein's suggestion
You don't need to segue into the second controller and then present. Instead, present directly instead of show the controller keeping it as rootViewController of a UINavigationController. Do this on the action:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let controller = UINavigationController(rootViewController:
TipJarViewController<TipJarOptions>())
present(controller, animated: false, completion: nil)
}
Edit: To add back button in TipJarViewController, here is the code:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(handleBack))
}
#objc func handleBack() {
dismiss(animated: true)
}
I want UINavigationBar's Topbar on the way swipe gesture.
My storyboard is like this:
LoginViewController(UINavigationController) =>(pushViewController) RegisterViewController
I tried some methods, The most similar answer is
LoginViewController's Navigation bar set to hidden. setNavigationBarHidden(true, animated: true)
RegisterViewController's Navigation bar set to show. self.navigationController?.setNavigationBarHidden(false, animated: true)
But, This method is must have LoginViewController's navigation bar set to hidden.
Is there another good way?
Result) https://puu.sh/Ei30r/14dc30d883.jpg
I want) https://puu.sh/Ei32K/437b731c80.jpg
Replace img tag with link because i have not at least 10 reputation.
Sorry,
I've tried this and it worked. You can give it a try.
on LoginViewController's
override func viewWillAppear(_ animated: Bool) {
navigationController?.setNavigationBarHidden(true, animated: true)
}
on RegisterViewController's
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
I want to be able to change the action of the back bar button item on a specific UIViewController in my navigation controller so that it pops to the root view controller. I've tried the following but they don't work:
let backButton = UIBarButtonItem(title: nil, style: .plain, target: self, action: #selector(back))
self.navigationItem.backBarButtonItem = backButton
and
self.navigationItem.backBarButtonItem?.action = #selector(back)
Any suggestions?
You should use self.navigationItem.leftBarButtonItem = backButton
Good luck
First of all backBarButtonItem action not works because you can only change back button title,take a look question about it here.
Solution
In ViewController from which you want to pop to root ViewController you need to set as a delegate of UINavigationControllerDelegate
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
}
and implement UINavigationControllerDelegate this method`
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if viewController.isKind(of:PreviousViewController.self) {
navigationController.popToRootViewController(animated: animated)
}
}
If my answer not fit your needs you can check similar question here.
To keep the same look and feel of the back button but change the action, see the ViewWillDisappear answer to the question regarding, "Execute action when back bar button of UINavigationController is pressed" Execute action when back bar button of UINavigationController is pressed
This is your solution, just need to set target and selector, nothing more.
private func setNavBar() {
let item = navigationItem.backBarButtonItem
item?.target = self
item?.action = #selector(self.donePressed)
navigationItem.leftBarButtonItem = item
}
#objc private func donePressed() {
self.dismiss(animated: true, completion: nil)
}
I want to have a back button on the second view controller I have. When it loads now, it does show the back button but I want to change the title from Back to something else. I implemented to viewWillAppear method where I invoke showing the navigation bar.
The following didn't work:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.navigationBar.hidden = false
self.navigationController?.navigationBar.backItem?.title = "Something Else"
}
Please help me change the title. Should it be in the willAppear or viewDidLoad?
Add this right before the push or popViewController statement in your first view:
let backButton = UIBarButtonItem(title: "Something Else",style: UIBarButtonItemStyle.Plain ,target: nil,action: nil)
self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton
Or you can do it in your second view this way:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated);
let backButton = UIBarButtonItem(title: "Something Else",style: UIBarButtonItemStyle.Plain ,target: nil,action: nil)
self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton
}
Ksa_coder I have tried the solution for your question and finally I got.You want to add the below coding in second view controller in viewWillAppear method
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated);
var btnBack = UIBarButtonItem(title: "Something Else",style: UIBarButtonItemStyle.Plain ,target: nil,action: nil)
self.navigationController!.navigationBar.topItem!.backBarButtonItem = btnBack
}