简体   繁体   English

点击选项卡栏时将tableview滚动到顶部

[英]Scroll tableview to the top when tapping tab bar

I need to scroll table view all the way up when selecting the tab bar item. 选择选项卡栏项时,我需要一直滚动表格视图。 I tried this but it does not work. 我试过了,但是没有用。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    let tabBarIndex = tabBarController.selectedIndex
    if tabBarIndex == 0 {

        let indexPath = NSIndexPath(row: 0, section: 0)
        MyViewController().tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
    }
}

The method is called but the tableView does not scroll to the top. 该方法被调用,但是tableView不会滚动到顶部。

The problem is you're creating a new MyViewController instance rather than accessing the one on screen. 问题是您要创建一个新的MyViewController实例,而不是访问屏幕上的实例。 You'll want to access the viewController already created and luckily the this delegate method hands that to you. 您将要访问已经创建的viewController,幸运的是,此委托方法会将其交给您。

Change this line 更改此行

MyViewController().tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)

to

let myViewController = viewController as? MyViewController
myViewController.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
self.tableviewObject.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 点击状态栏时滚动到顶部 - Scroll to top when tapping the status bar 通过点击状态栏无法工作(包含许多子视图)SWIFT滚动到顶部TableView - Scroll to top TableView by tapping status bar not work (with many subviews) SWIFT 点击屏幕顶部的状态栏时,UITableView会滚动到顶部 - UITableView scroll to top when tapping status bar at top of the screen 通过点击状态栏滚动到UITableView的顶部 - Scroll to top of UITableView by tapping status bar TableView 滚动底层顶部栏 - TableView scroll underlying top bar 如何像点击状态栏一样在 iOS 11 中调用缓慢滚动到顶部的动画? - How to invoke the slowly scroll to top animation in iOS 11 just like when tapping the status bar? 我可以在点击状态栏时禁用/编辑自动跳转到顶部滚动吗? - Can I disable / edit the automatic jump-to-top scroll when tapping status bar? SwiftUI:在横向列表中点击导航栏不会将列表滚动到顶部 - SwiftUI: Tapping navigation bar in landscape list does not scroll list to top 在导航栏上单击后退项目时更新tableview数据源 - Update tableview datasource when tapping back item on navigation bar 标签栏单击以滚动到顶部表格视图 - Tab bar click to scroll to top table view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM