Reputation: 29
I have a custom command here to scrollToTop if any tabBar item is pressed twice. but now i want to add a function to a specific tabBaritem ( tag 3 lets say....)
import UIKit
class TabBarController: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let viewControllers = viewControllers else { return false }
if viewController == viewControllers[selectedIndex] {
if let nav = viewController as? UINavigationController {
guard let topController = nav.viewControllers.last else { return true }
if !topController.isScrolledToTop {
topController.scrollToTop()
return false
} else {
nav.popViewController(animated: true)
}
return true
}
}
return true
}
}
extension UIViewController {
func scrollToTop() {
func scrollToTop(view: UIView?) {
guard let view = view else { return }
switch view {
case let scrollView as UIScrollView:
if scrollView.scrollsToTop == true {
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return
}
default:
break
}
for subView in view.subviews {
scrollToTop(view: subView)
}
}
scrollToTop(view: view)
}
var isScrolledToTop: Bool {
if self is UITableViewController {
return (self as! UITableViewController).tableView.contentOffset.y == 0
}
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
func tabBarController(_ tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if tabBarController.selectedIndex == 0 {
// present your first view controller
}
else if tabBarController.selectedIndex == 1 {
// present your second view controller
}
else if tabBarController.selectedIndex == 2 {
// present your third view controller
}
}
Thank you for your help...
Upvotes: 0
Views: 1327
Reputation: 333
Let me see if i get this right : you want to do something based on the item(first, second, etc) selected in the tab bar. If so, i think you can use this based on the tag of your item(if you set the delegate) :
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 1:
// your code
default:
// your code
}
}
And dont forget to set the tag of each one of your itens in the tab bar.
Upvotes: 0
Reputation: 6369
Try this code. Use tap gesture recognizer.
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer()
tap.numberOfTapsRequired = 2
tap.addTarget(self, action: #selector(MyTabBarController.twiceTapped(sender:)))
tabBar.addGestureRecognizer(tap)
}
func twiceTapped(sender: UITapGestureRecognizer) {
// handle here
}
}
Upvotes: 1