Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UIAbbarController始终打开UIViewController顶部的页面_Swift_Xcode_Uiviewcontroller_Uitabbarcontroller_Uitabbar - Fatal编程技术网

Swift UIAbbarController始终打开UIViewController顶部的页面

Swift UIAbbarController始终打开UIViewController顶部的页面,swift,xcode,uiviewcontroller,uitabbarcontroller,uitabbar,Swift,Xcode,Uiviewcontroller,Uitabbarcontroller,Uitabbar,我最近在代码中实现了一种滚动到UIViewController顶部的方法,方法是轻触uitabar中的图标两次。代码位于我的UITabBarControllercode中。它工作得很好,但我发现不幸的副作用是,每次我打开应用程序上的页面时,它现在都位于UIViewController的顶部,而不是我上次停止的位置。我肯定这段代码中有错误 import UIKit class TabViewController: UITabBarController, UITabBarController

我最近在代码中实现了一种滚动到
UIViewController
顶部的方法,方法是轻触
uitabar
中的图标两次。代码位于我的
UITabBarController
code中。它工作得很好,但我发现不幸的副作用是,每次我打开应用程序上的页面时,它现在都位于
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

因此,到
视图将出现
添加此行以重置:

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

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