像Facebook一样向下滚动后如何从顶部滚动-Swift 4.2

时间:2018-08-10 10:20:51

标签: ios swift tableview

我想要实现Facebook应用程序之类的滚动功能,如果向下滚动页面,请点击Tab栏项目,然后点击同一标签栏项,它会从顶部开始滚动。

这是我的代码-

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        let indexOfTab = tabBar.items?.index(of: item)
        if indexOfTab == 0{
            let feed = HomeFeedViewController()
            feed.scrollToTop()
            print("pressed tabBar: \(String(describing: indexOfTab))")
        }else if indexOfTab == 1{
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }else if indexOfTab == 2{
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }else if indexOfTab == 3{
            let feed = QueueViewController()
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }else if indexOfTab == 4{
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }

    }


 func scrollToTop(){

        feedsTableView.setContentOffset(.zero, animated: true)
    }

3 个答案:

答案 0 :(得分:0)

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        let indexOfTab = tabBar.items?.index(of: item)
        if indexOfTab == 0{
            self.scrollToTop()
            print("pressed tabBar: \(String(describing: indexOfTab))")
        }else if indexOfTab == 1{
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }else if indexOfTab == 2{
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }else if indexOfTab == 3{
            let feed = QueueViewController()
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }else if indexOfTab == 4{
            print("pressed tabBar: \(String(describing: indexOfTab))")

        }

    }


 func scrollToTop(){
self.feedsTableView.setContentOffset(CGPoint.zero, animated: true)
self.feedsTableView.layoutIfNeeded()
    }

答案 1 :(得分:0)

请尝试输入我的代码

 Sub Main

  host = Array("host1", "host2")

  For each item in host

  Dim user
  user = "Username"


  Dim passwd
  passwd = "password"

  cmd = "/SSH2 /L " & user & " /PASSWORD " & passwd & " /C AES-192-CTR /M SHA2-256 " & item

  crt.Session.Connect cmd

  Dim tab, index, nTabCount

    nTabCount = crt.GetTabCount()

    for index = 1 to nTabCount
        set tab = crt.GetTab(index)
        tab.activate
        tab.screen.send "command" & vbcr
        crt.Sleep 5000
        crt.Session.Disconnect()
    next

  Next


End Sub

答案 2 :(得分:0)

对于Swift

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: self.view)
    }
}

要使用

var previousController: UIViewController?

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        //when user is in FeedViewController and scroll at last record and want to
        if previousController == viewController {
            if let navVC = viewController as? UINavigationController, let vc = navVC.viewControllers.first as? HomeViewController {

                if vc.isViewLoaded && (vc.view.window != nil) {
                    vc.scrollToTop()
                }
                print("same")
            }
        }
        else{
            print("No same")
        }

        previousController = viewController
        return true;
    }