I have a tab controller which is displaying my home screen. In my home screen, there are optional filters for my collection view there. I would like the filters to be reset if the user taps on the same tab bar button. Currently, tapping on the tab bar button doesn't do anything if you are already in the same view of the tab bar button. How can I execute code if the tab bar button was tapped a second time?
Try this method in a UITabBarController subclass or put tabbar delegate in your controller.
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
var isStatusbarWhite = false
for xx in (tabBar.items as! [UITabBarItem]){
for var i:Int = 0 ; i < self.tabBar.items?.count ; ++i {
let xx = tabBar.items![i] as! UITabBarItem
if (xx == item){
if tabBarController.selectedIndex == i {
//YOUR CODE HERE SAME TAB SELECTED
}
}
}
}
Related
I have 5 tab bar items in my tab bar, 4 of which have segues to navigation controllers which lead to view controllers. I want to make the middle tab bar item act as a button, so that when I click on it, I have control over what happens.
Currently, my middle tab bar item is also connected to a navigation controller, which is not right because now when I click the tab bar item, it opens a black navigation controller. How can I convert the middle tab bar item to act as a button, rather than going to a navigation controller?
If I remove the navigation controller, it also removes the tab bar item from the tab bar.
If you want your tab bar item to act as a button you could subclass a UITabBarController and implement this delegate function:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// Check if bar item selected is center
if viewController.tabBarItem.tag == 2 {
// Do Something Here ...
// Present View Controller
guard let navigationController = storyboard?.instantiateViewController(withIdentifier: "NavigationController") as? UINavigationController else { return false }
present(navigationController, animated: true)
// Returning false will not open the connected tab bar item view controller you attached to it
return false
}
// Return true to open the connected tab bar item view controller you attached to it (e.x. everything but the center item)
return true
}
To implement a custom tab bar and use tab bar items like a normal button
Add a new Tab Bar view to your view controller (VC)
Add the custom tab bar items you need and assign tag numbers on them (On Attribute inspector)
Add a delegate outlet from Tab bar View to your view controller (Right Click and drag To VC)
On your view controller, subclass UITabBarDelegate, like this
class ViewController: UIViewController, UITaBarDelegate {}
Implement this delegate function, then it should works
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 1 {
//Do something
}
if item.tag == 2 {
//Do something
}
.......
}
I am working on a e-commerce project which has a requirement of having both TabBar and Navigation slider in the application. I have managed to achieve both functionality, but when the app Launches only one tab (Default Home tab) is selected until i navigation to next screens from home tab and come back to home screen i cannot able to switch to the other tabs first time.
Here is the Hierarchy of view controller i have made.
-> Base View controller -> Tab bar and Navigation Sliders (as Childe View controllers)
TabBarController hierarchy :
TabBarController -> NavigationController 1 -> View Controller like this i have 5 tabs with the above combination.
Someone please help me to come out form this issue.
I can't able to tab any tabs in the screen until i tab any one of the cells in this screen.
Below is the code i am using to set the Navigation slider and tab bar controller respectively.
///Controller to display Menu Slider
fileprivate var masterViewController: MyMenuTableViewController? {
willSet {
if self.masterViewController != nil {
self.masterViewController?.view.removeFromSuperview()
self.masterViewController?.removeFromParentViewController()
}
} didSet {
if self.masterViewController != nil {
self.view.addSubview(self.masterViewController!.view)
self.addChildViewController(self.masterViewController!)
}
}
}
///To display TaBar Controller
fileprivate var detailViewController: HomeViewController? {
willSet {
if self.detailViewController != nil {
self.detailViewController?.view.removeFromSuperview()
self.detailViewController?.removeFromParentViewController()
}
} didSet {
if self.detailViewController != nil {
self.view.addSubview(self.detailViewController!.view)
self.addChildViewController(self.detailViewController!)
}
}
}
I have a 5 tab TabBarController, with selectedIndex = 2 (3rd TabBarItem) being a full-screen camera embedded within a NavigationViewController with the Tab Bar and Navigation Bar hidden via self.tabBarController?.tabBar.isHidden = true and self.navigationController?.isNavigationBarHidden = true respectively.
I have a UIButton presented over the camera preview layer; currently, I'm implementing self.tabBarController?.selectedIndex = 0 to navigate back to the 1st TabBarItem.
What I need: How do I store the last selectedIndex (before selecting the Camera Tab) so that the UIButton returns to user to the last TabBarView before initializing the camera?
Keep in mind the camera is presented by TabBarItem (selectedIndex = 2) so its not presenting a modal view on top.
Logic Example:
VC1 > VC3 (Camera) > Select UIButton > VC1
VC2 > VC5 > VC3 (Camera) > Select UIButton > VC5
As an aside, I've implemented the following function in my TabBarController (subclassed with UITabBarControllerDelegate) to record the selectedIndex.
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("The previous tab was index \(selectedIndex)")
}
The easiest solution is to save that index in UserDefaults in the viewWillDisappear of the five ViewControllers and retrieve it from when you click the button to navigate to that index
(Posted on behalf of the OP).
Thanks to the suggestions I was able to make the process work. Implementation code below:
In TabBarController
overrride fun tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the previous tab was index \(selectedIndex)")
UserDefaults.standard.set(selectedIndex, forKey: "previousTab")
UserDefaults.standard.synchronize()
}
Within the UIButton on the CameraViewController
#IBAction func exitCamera(_ sender: UIButton) {
if let lastTab = UserDefaults.standard.string(forKey: "previousTab") {
self.tabBarController?.selectedIndex = Int(lastTab)!
DispatchQueue.main.async {
self.stopCaptureSession()
}
}
}
So far it works like a charm! :-)
I have created a centre TabBar button by subclassing UITabBarController and adding a bigger button to the centre.
Now when i go to an item in my menu, which is just a static TableViewController embedded in a ViewController the button will not hide with the TabBar, where i have just set the bottom bar to hide on push within interface builder.
Static Table View
Button Still Shown when menu item page loaded. This is just an empty ViewController with a 'show' segue from the static TableViewCell
Hid Bottom Bar Selected.
i also have a problem when going back to the menu and the TabBar is displayed again. The button is now behind the TabBar.
------EDIT-------
I revised my outlook and have added the following to my TabBarController class
override func viewDidLayoutSubviews() {
if self.homeButton != nil {
self.view.bringSubview(toFront: self.homeButton)
for test in self.view.subviews {
if let subView = test as? UITabBar {
if subView.isHidden == true {
self.homeButton.isHidden = true
} else {
self.homeButton.isHidden = false
}
}
}
}
}
its just a little slow, there is a delay of about 1 second from when the view changes to when the button is wither hidden or brought to the front.
Any better ideas about how to display this faster?
I have a View Controller with a UITabBar on the bottom. This tab bar performs a different function depending on which tab is selected. I would like to have one of the buttons selected when the view controller loads. I can't find a solution that doesn't involve a tabbarviewcontroller
class ViewController: UIViewController, UITabBarDelegate {
#IBOutlet var tabBar : UITabBar
//In view controller declare a UITabBar as an Outlet
override func viewDidLoad() {
super.viewDidLoad()
tabBar.selectedItem = tabBar.items[0] as UITabBarItem;// here you
can load the default button option which you want to load
}
func tabBar(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) {
var tabBarValue = 0;
switch item.tag {
case 0:
tabBarValue = // assign value
break
case 1:
tabBarValue = // assign value
break
default:
break
}}
Pl. refer to the below url also, where they try to load different webview for the selected tab, Use a UITabBar without UITabBarController to control a UIWebView
This was the solution that worked out for me.
for i in tabBar.items as [UITabBarItem] {
if i.tag == 0 {
tabBar.selectedItem = i
}