我在这里有一个自定义命令,可以在任何选项卡栏项被按下两次时滚动到顶部。但现在我想在一个特定的tabBaritem中添加一个函数(标签3让我们说…)
我该如何结合我现在所拥有的:

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

有了这个(这样我就可以对特定的tabBarItem执行特定操作):
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
    }

}

谢谢你的帮助。。。

最佳答案

试试这个代码。使用轻敲手势识别器。

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

关于ios - 点击tabBarItem以显示 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44815024/

10-11 14:49