点击TabBarItem以scrollToTop

时间:2017-03-04 22:30:29

标签: ios swift uitableview uitabbarcontroller scrolltop

怎么做?

我尝试了在堆栈溢出时可以找到的所有内容,但对我来说没有任何效果。我想要一个像Instagram一样的解决方案。如果您向下滚动,请点击TabBarItem以转到顶部。(我通常使用tableviewControllers而不查看控制器,如果这对代码很重要)

任何帮助将不胜感激

在swift 2.2中谢谢你!

我尝试了什么:

//Using the UiTabBarDelegate
class BarsViewController: UITableViewController,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate,UITabBarControllerDelegate,UITabBarDelegate{

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
            if(item.tag == 1) {
                //your code for tab item 1

            tableView.scrollToTop(true)

            }
            else if(item.tag == 2) {
                //your code for tab item 2
            }
            else if(item.tag == 4) {
                //your code for tab item 4
                self.tabBarItem.badgeValue = nil

                tableView.scrollToTop(true)        }
        }
    }

    extension UITableView {
        func scrollToTop(animated: Bool) {
            setContentOffset(CGPoint.zero, animated: animated)
        }
    }

2 个答案:

答案 0 :(得分:1)

在按钮操作中,拨打tableView.scrollToRow(IndexPath(row: 0, section:0), at scrollPosition: .top, animated: true)

答案 1 :(得分:0)

选择您的tableView,然后查看右侧的attributes inspector,确保选中scrollsToTop或尝试self.yourTableView.scrollsToTop = true

如果您想在动作上滚动到顶部,请创建一个像这样的扩展名

extension UITableView {
func scrollToTop(animated: Bool) {
    setContentOffset(CGPoint.zero, animated: animated)
 }
}

然后在tabbarbutton中调用此函数,

tableView.scrollToTop(animated: true)

这是快速3,但我收到了这个答案的信息并写在早期版本的swift中, scroll to top of tableView