UITabBarController始终在UIViewController顶部打开页面

时间:2018-11-13 16:26:39

标签: swift xcode uiviewcontroller uitabbarcontroller uitabbar

我最近在我的代码中实现了一种方法,可以通过点击UIViewController中的一个图标两次来滚动到UITabBar的顶部。该代码位于我的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
    }
}

1 个答案:

答案 0 :(得分:2)

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

因此在viewWillAppear上添加以下行以重设此行:

pressedCount = 0

还修复了每次用户两次按下tabBar项目时,tabBar didSelect项目中的语句是否重置pressedCount

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