I am troubling in hide UITabBarController inside the TabBar item ChildViewControllers
For Ex. Suppose We have two tab bar item in my home screen and first tab bar item is selected and i want to go with navigate first tab bar then i want to hide TabBar in first TabBar item childViewControllers
I found solution for this
[self.tabBarController.tabBar setHidden:YES];
use hide Tab bar item in viewDidLoad using hidden property.
And Select Under Opaque Bars option in storyBoard ViewController.
before view is pushed or showed. hidesBottomBarWhenPushed variable on viewcontroll will be checked and automaticlly hides bottom bar. you can use it in two ways:
1- override it in child controllers:
override var hidesBottomBarWhenPushed: Bool {
return true
}
2- you can set it before performing segue in prepare for segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier" {
segue.destination.hidesBottomBarWhenPushed = true
}
}
Related
I have an InvoiceVC in the second tab bar ( tab Bar index : 1 ) like the picture above. if the table view cell is tapped, I need to segue to the InvoiceDetailVC like the picture below
as you can see in the InvoiceDetailVC, there is no tab bar in the bottom of the InvoiceDetailVC, I mean that tab that has red badge in the bottom. I need remove that tab bar.
I have tried to use present modally instead of push show segue. But the problem is, there is no back button to back to InvoiceVC
so what should I do ?
Use in prepareforsegue while pushing and set hidesBottomBarWhenPushed to true to hide the tabbar on destination view controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "InvoiceVC") {
let indexPath: IndexPath? = tableView.indexPathForSelectedRow
let destViewController = segue.destination as? InvoiceVC
destViewController?.recipeName = recipes[indexPath?.row ?? 0]
destViewController?.hidesBottomBarWhenPushed = true
}
}
I have embedded a viewController in a NavigationController and set it as the rootViewController. Then I connected the TabBarController to the NavigationController. I have a button in the LessonViewController that shows the PurchaseViewController, and then a back button in the PurchaseViewController which shows the LessonViewController. However, the tab bar was still present in the PurchaseViewController so I ticked hideBottomBarOnPush, which solved this problem, however, when I segued back to the LessonViewController the tab bar had disappeared.
Any ideas?
The following image is what my storyboard looks like now:
Similar to barb’s code, I got this to work, while enabling “hide bottom toolbar when pushed” and then popping the view controller:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = false
}
You should do following way,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Your_Identifier" {
hidesBottomBarWhenPushed = true
DispatchQueue.main.async { self.hidesBottomBarWhenPushed = false }
}
}
It will show TabBar reappears while segue back.
I have a view controller that is loaded up to display some information from a table view cell in a table view which is in a tab bar controller (tab 2). I want to return to tab 1, how can I do this in swift?
I've tried this, but it doesn't seem to work
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//TODO: Return to the tab bar
if segue.identifier == "returnToTab"{
print("prepare for segue called!")
tabBarController?.selectedIndex = 2
}
}
You can use:
self.tabBarController?.selectedIndex = 0 //(To return to your first controller)
I have no problem actually performing the segue, but when I do, my tab bar disappears from the bottom of the view. I have made a storyboard segue from TabBarController1 to TabBarController2.
I've found a lot of answers for Objective-C, but none for Swift.
This is the code for performing the segue:
if requestsArray.count == 0 {
self.performSegueWithIdentifier("offerSegue", sender: self)
} else {
self.performSegueWithIdentifier("confirm1", sender: self)
}
You don't want to segue. A segue creates a new instance of the destination view controller and presents it.
That's why your tab bar is disappearing. You are covering your tab bar controller, with it's 2 tabs, with a new instance of your TabBarController2.
You want to switch to the other tab.
What you want to do is to ask your owning tab bar controller to switch tabs.
UIViewController has a property tabBarController that lets you get to your owning tab bar controller.
TabBarControllers have a property selectedIndex that let you select one of a tab bar controller's view controllers to become the active view controller.
So, send a message to your tab bar controller asking it to switch to the other tab.
EDIT:
Other people aside from the OP have asked for sample code illustrating how to do this. I decided to create a sample project illustrating how to do it.
You can download it from Github: https://github.com/DuncanMC/TabBarControllers.git
I created a base class of UIViewController ATabController for the view controllers that are managed by the tab bar controller. The ATabController.swift file includes an enum to indicate which tab you want to select:
#objc enum Tab: Int {
case first = 0
case second
case third
}
(Note that the enum has to be an Objective-C enum if you're going to pass parameters of type Tab to IBActions, since IBAction methods need to use Objective-C types and function signatures.)
It also includes a protocol TabController:
#objc protocol TabController {
#objc func switchTab(to: Tab)
}
It also defines a delegate tabDelegate:
weak var tabDelegate: TabController?
The tab bar controller has a prepareForSegue (prepare(for:sender:)) that it uses to make itself the tabDelegate of all the view controllers it manages as tabs:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let child = segue.destination as? ATabController {
child.tabDelegate = self
}
And then it implements the switchTab(to:) method:
#objc func switchTab(to: Tab) {
let index = to.rawValue
guard let viewControllerCount = viewControllers?.count,
index >= 0 && index < viewControllerCount else { return }
selectedIndex = index
}
In any of the child view controllers that are tabs of the tab bar controller, you can use IBAction code like this to switch tabs:
#IBAction func handleFirstButton(_ sender: Any) {
tabDelegate?.switchTab(to: .first)
}
If you're looking for how to change from one tab to another in a tab controller without using the tab bar you can do this
tabBarController?.selectedIndex = [number of tab]
I set up a series of onboarding scenes for my app using this tutorial, and elsewhere in my app use a TabViewController. When I run the app, the Onboarding scenes have a blank tab bar at the bottom.
I've tried ticking "Hide Bottom Bar on Push" from all 3 views on interface builder:
I've also tried adding
self.hidesBottomBarWhenPushed = true to the override func viewDidLoad() functions of the ViewController, PageViewController and PageContentViewController. No joy!
Also tried self.tabBarController?.tabBar.hidden = true in the same places.
Elsewhere I've found references to this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLogin"{
println("showLogin seque called")
let bottomBar = segue.destinationViewController as LoginViewController
bottomBar.hidesBottomBarWhenPushed = true
bottomBar.navigationItem.hidesBackButton = true
}
}
but, I don't have any named segues, so not sure how to identify the segue.
Any help would be amazing!
Thanks
To give the Segue name, first choose the segue and then go to Attributes Inspector.
Below there you find Storyboard Segue, from there you provide Identifier name in Identifier Box.
Thanks