Reputation: 117
I've tried adding a gesture recogniser, not realising that it isn't possible with a UITabBarItem, here is what I did:
Any other suggestions?
Upvotes: 1
Views: 1838
Reputation: 2725
One simple solution can be like this.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let name = viewController.tabBarItem.title ?? ""
if(name == "Home"){
if(name == self.selectedTabName){
print("tapped home again")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "scrollToTop"), object: nil)
}
}
self.selectedTabName = name
self.delay(0.5){
self.selectedTabName = ""
}
}
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
Upvotes: 0
Reputation: 1876
You'll see that error whenever you try to add a gesture recognizer to an object that doesn't inherit from UIView. Try adding the gesture recognizer to the UITabBar and then using the point where the double tap occurred, matching it to the correct tab bar item.
Upvotes: 1
Reputation: 1131
I don't think this will have a simple solution, as double tapping a tab bar item is not expected behavior. Even if you got it working, I would assume Apple would not pass the review, as it will likely go against their design guidelines.
Upvotes: 2