HsoiEnterprises / HELargeCenterTabBarController

A Swift UITabBarController with a larger center tab.
Other
45 stars 7 forks source link

Unable to switch to other tab, if set allowSwitch to true the target action is triggered for other tab #6

Closed jordanhbuiltbyhq closed 8 years ago

jordanhbuiltbyhq commented 8 years ago

I have set up a tab bar controller using Storyboard that contains two view controllers. I want to allow switching between these two controllers as usual. When you tap the center button I want to use target action to present a new screen modally.

When I add the center button, I set allowSwitch to false. When I do this, tapping on the second tab does nothing - it doesn't switch to show that tab, the first tab remains selected. If I set allowSwitch to true it will properly switch to the second tab, but it will also trigger the action therefore the modal appears when it shouldn't. Have I done something wrong? I'm using iOS 9.3.

dispatch_async(dispatch_get_main_queue()) { let centerTabBar = self.window!.rootViewController as! HELargeCenterTabBarController centerTabBar.addCenterButton(unselectedImage: UIImage(named: "tab-send")!, selectedImage: UIImage(named: "tab-send")!, target: self, action: #selector(self.didTapSendVyte), allowSwitch: true) }

jordanhbuiltbyhq commented 8 years ago

I was able to fix this by commenting out centerButtonAction(centerButton) in tabBarController:didSelectViewController:) and if allowSwitch { selectedViewController = centerViewController } in centerButtonAction. Is this the expected behavior?

hsoi commented 8 years ago

What you want to do is actually set up your UITabBarController to have 3 tabs.

So what you have now: tab 1 and tab 2.... they would become tab 1 and tab 3.

Then you make a dummy/placeholder/ignored UIViewController for tab 2. You'll never switch to it, it doesn't have to actually have or do anything. But it then lets the center button work out.

Let me know if that gets things going or not.

jordanhbuiltbyhq commented 8 years ago

Oh I see. Yep that works. Thanks much!

hsoi commented 8 years ago

Good deal! 👍