2

I am pretty much trying to replicate the same tabBar popup as you see in the yelp app (before and after screenshots at the bottom) where no matter what view your in you can press on the center tabBar item and pop up will appear.

Coincidentally I have 5 tabBar items (like Yelp) and I am trying to have three popups with a image and title for each (like Yelp). Seeing that what I am trying to do is already done in an app shows me that this is possible, but I do not know how to do it. I have tried to change the types of relationships between view controllers or do it programmatically, but nothing seems to work. What am I missing or doing wrong?

Tabbar Controller Code:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController.tabBarItem.tag == 1 {
        return false
    } else {
        return true
    }
}

Before Press

Before Press

After Press

After Press

Vinodh
  • 5,262
  • 4
  • 38
  • 68
ap123
  • 916
  • 1
  • 8
  • 22

1 Answers1

3

One solution could be to adopt the UITabBarControllerDelegate.

This way we can use the func tabBarController(UITabBarController, didSelect: UIViewController) method of the delegate to alter the regular presentation behavior and show our popups instead. We could find the offset required (in terms of CGPoint) relative to the tab bar button and then apply that offset and add the popup buttons to as a subview. Note that for this method you would need to programmatically set the frames of your popup buttons.

Alternatively, you could also make a bunch of popup buttons and set their alphas to zero and one when clicked. Hope this helped! Thanks :)

Mihir Thanekar
  • 508
  • 4
  • 8
  • I will love to implement this in my code, preferably the second one as it seems easier to add, but I seem to be having trouble with it. I am using your first link and am using the `shouldSelect` method in my `UITabBarController` where I gave the middle tabbar item a tag of 1 and in the method I added `if viewController.tabBarItem.tag == 1 { return false } else { return true }` but when the user presses on the middle item it still transports to that view. What am I doing wrong? – ap123 Aug 22 '17 at 21:14
  • @AlessandroProspato, could you provide us with a snippet of your code or something that I can see so as to validate what's going on? **EDIT**: `Should select` is to transport to the view, so if you want to stay on the same view, return false. Don't do anything else in that method. DidSelect will have your popups. ShouldSelect will stop the transport. See the return value section of: https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate/1621166-tabbarcontroller – Mihir Thanekar Aug 22 '17 at 21:19
  • Thank you for the fast response! I have added my code for the `shoudSelect`. For some reason with this code it is still transporting me. Also I have not added anything to `didSelect` as I am not sure what to place in it yet – ap123 Aug 22 '17 at 21:37
  • This is a serious problem that I will like to fix, but I have no idea how. Any further assistance is greatly appreciated – ap123 Aug 23 '17 at 03:05
  • the only code I made for this is what I have in the question. I set break points at the `if` and `else` statements, but they were not called when they were pressed – ap123 Aug 26 '17 at 03:42
  • Sadly no. It has not been solved @lilbiscuit. Let me know if you have any insights – ap123 Jun 20 '18 at 21:52
  • @ap123 I am using [tabBarController:shouldSelectViewController:](https://developer.apple.com/documentation/uikit/uitabbarcontrollerdelegate/1621166-tabbarcontroller?language=objc) with success. – lilbiscuit Jun 23 '18 at 15:05
  • @lilbiscuit where did you place your `shouldSelectViewController` method? I tried placing it in both the tabBarController and in the viewController and set print statements and breakpoints in side the method but none of them got called. What am I missing? – ap123 Jun 24 '18 at 20:56
  • @ap123 I am using this in a NativeScript app so my code won't be relevant. For debugging did you remote `return true` completely just to see what happens? – lilbiscuit Jun 25 '18 at 21:45