UITabBarController 始终在 UIViewController 顶部打开页面

UITabBarController always open page at the top of UIViewController

我最近在我的代码中实现了一种通过点击 UITabBar 中的图标两次滚动到 UIViewController 顶部的方法。该代码位于我的 UITabBarController 代码中。它工作得很好,但是我发现不幸的副作用是每次我在我的应用程序上打开一个页面时,它现在位于 UIViewController 的顶部,而不是我上次离开的地方。我确定这段代码中某处有错误

   import UIKit

class TabViewController: UITabBarController, UITabBarControllerDelegate {
    var pressedCount: Int = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self


        // Do any additional setup after loading the view.
    }
    @IBAction func unwindToMain(segue: UIStoryboardSegue) {}

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)

        self.navigationController?.isNavigationBarHidden = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        pressedCount += 1
        if pressedCount > 1 {
            scrollToTop()
        } else {
            //do something for first press
        }
        print("Selected item")
    }

    // UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        print("Selected view controller")
    }
    func tabBarController(_ TabViewController: 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
    }
}

问题是,当您的 ViewController 消失时,它仍然 pressedCount 属性 设置为 2

所以要 viewWillAppear 添加此行以重置它:

pressedCount = 0

还修复了 tabBar didSelect 项中的 if 语句以在用户每次按 tabBar 项两次时重置 pressedCount

if pressedCount > 1 {
    scrollToTop()
    pressedCount = 0
} else {
    //do something for first press
}