Detecting second tap on tab bar item when first one triggers popToRootViewController - ios

I want to implement scrolling to top of view controller when user taps on tab bar item in tab bar. I do it by overriding tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool method in UITabBarControllerDelegate.
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// Scroll to top only if we are already on given tab
if viewControllers?.firstIndex(of: viewController) == selectedIndex {
// Checks if root VC of navigation controller is currently shown and then scrolls it to top
scrollToTop(at: selectedIndex)
}
return true
}
Each tab has navigation controller as its view controller and tapping on tab bar causes pop to root view controller. It's fine and desired, but I want a double tap to work, i.e. user is in second (or later) view in navigation controller, double taps on tab bar and then navigation controller pops and root view controller scrolls to top (first tap pops to root view controller and second one scrolls to top).
Unfortunately, this delegate method is called only once when such scenario happens. It starts detecting next taps on tab bar items only after pop transition ends.
Double tapping works fine when switching tabs, i.e. user is on tab B, double taps tab A, and then first tap switches to tab A and second one scrolls makes it scroll to top.
I checked Facebook and Twitter apps, and both of them correctly handle quick double tap - they pop and then scroll to top.
Is there a way to detect tab bar items taps when pop transition is in progress? I tried overriding tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) of UITabBarController but it's also not called twice on double tap in aforementioned scenario. Adding custom double tap recognizer also does not help as it significantly delays single taps to tab bar which causes app to look laggy.

You can try using Notification for this case.
First, create extension for your Notification.Name :
extension Notification.Name {
static var userDoubleTapTabbar: Notification.Name {
return .init(rawValue: "User.DoubleTapTabbar")
}
}
Then, In your Tabbar Controller, detect your user double tap items event and fire notification:
public var lastTabbarItem: UITabBarItem?
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
SEManager.shared.playSound(type: .tap_bottom_tap)
if self.lastTabbarItem == item {
NotificationCenter.default.post(name: .userDoubleTapTabbar, object: nil)
}
self.lastTabbarItem = item
}
Then in your Tabbar item view controller, you can add observer & handler for that notification to scroll your table view to top. You can use this to check if that Tabbar item view controller is current visible:
if viewController.viewIfLoaded?.window != nil {
// viewController is visible
}
Hope it helps and sorry for my bad English.

Related

iOS Black screen when close modally presented view controller

I have a tabbar controller and suppose if the user goes to 3rd tab and in 3rd view controller, User presses a button and there I present the view controller as modal .
Now if user switched the tab let say tab 1 or 2 without closing the modally presented view controller, and mean while he comes back to tab 3, the user will still able to see the modally presented view controller, now if user closes the modally presented view controller here, then there is a black screen instead of view controller that opened the modally presented view controller.
The possible work around is that I hide the tabbar controller so that user must close modally presented the view controller first. but it is not the requirement right now. Due to some reasons I can not hide bottom tab bar controller.
Please suggest me how to do following
First of all when user switches tab after opening modally presented view controller, so when he closes the modally presented vc he must see the screen at the back
If point no 1 is not possible so when user goes back again to tab 3 (in which he started modally presented VC) there should not be modally presented view controller. In other words when user switched the tab and the modally presented view controller is opened, it must close, before moving to other tab.
You can do it with UITabBarControllerDelegate.
Option 1: Creating subclass of UITabBarController
class MyTabBar: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let currentlySelectedViewController = self.viewControllers?[self.selectedIndex] as? YourThirdTabViewController,
let presentedViewController = currentlySelectedViewController.presentedViewController {
presentedViewController.dismiss(animated: false, completion: nil)
}
return true
}
}
Here I am assuming that I have my own sub class of UITabBarController which is set as UITabBarControllerDelegate using self.delegate = self in view didLoad.
Option 2: Make TabBarController's ViewController UITabBarControllerDelegate in their ViewDidLoad
If you dont wanna have your own subclass of UITabBarController all you have to do is simply set your self as UITabBarControllerDelegate in each ViewController's ViewDidLoad
You can have a common protocol or extension to UIViewController and confirm UITabBarControllerDelegate to avoid code duplication if you decide to go down the path of not subclassing UITabBarController
protocol TabBarControllerProtocol: UITabBarControllerDelegate where Self: UIViewController {}
extension TabBarControllerProtocol {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let currentlySelectedViewController = self.tabBarController?.viewControllers?[self.tabBarController?.selectedIndex ?? 0] as? SomeViewController,
let presentedViewController = currentlySelectedViewController.presentedViewController {
presentedViewController.dismiss(animated: false, completion: nil)
}
return true
}
}
In whichever ViewController, you want this logic to kick in you can say
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
}
}
extension SomeViewController: TabBarControllerProtocol {}
Thats it.
shouldSelect viewController gets called every time user taps on tab to switch.
The if condition checks if ViewController at selectedIndex (already selected not the new one) is viewController of specific interest or not and also checks if this view controller has presented anything or not, if it has presented it will call dismiss on it
This way when user switches tab from 3rd tab to any other tab, if third tab VC has presented another view controller modally it will dismiss it before it switches tab.

Tab bar item as button

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
}
.......
}

Action not working if UITabBar has more than 5 items

I want to perform an action when the user clicked on some tabs in UITabBar without opening another view. For example, setting tab or share.
Here is what I did :
class ViewTabBarController: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("do something")
}
}
This code works fine if the UITabBar has just 5 items.
But the problem here is that if UITabBar has more than 5 items those that are under the "More Tab" did not call the tabBar() function when clicked.
From the documentation description of the didSelect method of UITabBarControllerDelegate:
Tells the delegate that the user selected an item in the tab bar.
What that means is that the method is called when the user taps on one of the buttons in the bar. When you have a 'more' button then that is the button in the tab bar so tapping 'more' fires that method.
The view controllers in the 'more' section are actually processed in a different way involving the use of a UINavigationController. With these views they don't have a button in the tab bar and therefore this even is not fired.

How to force a tab bar item to go to the root level of that item whenever its tapped

I'm new to iOS and Swift. In my storyboard I've set up a tab bar controller where the right most item is a "More" item that leads to a table view controller embedded in a navigation controller. Each item in the table view (static) goes to a view controller (say "View 1", "View 2", and "View 3").
The default behavior is as follows. Let's say I tap "More". Then I'm looking at the table with cells for "View 1", "View 2", and "View 3". Then let's say I tap "View 1". If I then click a different item in the tab bar controller, and then click "More" again, rather than take me to the table view, it will take me to "View 1" since that's the last thing I tapped when I was last in "More". I'd like the behavior to be that if I tap outside of "More" with another tab bar element, any time I tap "More" again, I want it to always take me to the table view, regardless of what I was viewing previously within "More".
Essentially, I want "More" to forget its state or reset its state.
How do I force this to happen?
I will walk you through an example to make sure that it is clear.
Consider that this is the storyboard:
It has a tabbar view controller connected to:
A view controller (the one on the top).
A table view controller which is embeded in navigation controller (the one on the bottom). It has tableview with a static cell, when tap on it, it pushes to another view controller.
So far so good! now, the first view controller class should conforms to UITabBarControllerDelegate and implements tabBarController(_:didSelect:) method, as follows:
class ViewController: UIViewController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// im my example the desired view controller is the second one
// it might be different in your case...
let secondVC = tabBarController.viewControllers?[1] as! UINavigationController
secondVC.popToRootViewController(animated: false)
}
}
Remark: if you have more that tow view controllers connected to the tabbar view controller, you should apply this code to the first view controller that should appear in the tabbar view controller.
And that's it!
Output:
Cheers up! hope this helped.
The code supplied works very well with one exception. If you have multiple tabbar items linked to one view controller then the indexed code tabBarController.viewControllers?[1] will only work on "1". It is possible to have "2" and "3" etc. By adding tabBarController.selectedIndex as the index it will always return back to root for that tabbar item. Below is how I fixed the code. I hope this helps someone:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// im my example the desired view controller is the second one
// it might be different in your case...
let secondVC = tabBarController.viewControllers?[tabBarController.selectedIndex] as! UINavigationController
secondVC.popToRootViewController(animated: false)
}
after making the class confirm to the tabBarController delegate just add this function
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
self.navigationController?.popToRootViewController(animated: false)
}

Can a Tab Bar contain a "play" button

In my Tab Bar, I have a "My favorites" and "About Us" icon. These go to different View Controllers. I have made a UIButton that plays an audio file. How do I add that button to the Tab Bar at the bottom?
So when the button is tapped, it plays on the current scene, and does not go to a new scene.
You can prevent a tab from opening it's view controller by subclassing UITabBarController and implementing tabBarController(_:shouldSelectViewController:). From there you could perform your play action.
More on UITabBarControllerDelegate
In Swift your subclass would look like the following
class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let tabIndex = tabBarController.viewControllers?.indexOf(viewController)
if tabIndex == 2 {
// Perform your play action here
return false
}
return true
}
}
You can write a your tabBarController which has several buttons. each button has a tag. so, you can judge the button which you can't go to a new controller.

Resources