我正在使用此View层次结构。
UITabBarController
> UINavigationController
> UITableViewController
-> UIViewController
(单击任何表格单元格)
我想在单击当前选定的选项卡栏项目时滚动到列表顶部。 我该如何实现?预先感谢。
答案 0 :(得分:0)
首先,您需要声明一个协议
protocol RefreshProtocol {
func scrollToTopRefresh()
}
然后您需要编写自定义tabBar控制器的委托方法
class MainViewController: UITabBarController {}
extension MainViewController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedViewController == viewController {
if let navController = viewController as? UINavigationController {
if let myViewController = navController.topViewController , let homeController = myViewController as? RefreshProtocol {
homeController.scrollToTopRefresh()
}
}
else {
if let homeController = viewController as? RefreshProtocol {
homeController.scrollToTopRefresh()
}
}
}
}
哪个类确认刷新协议将实现此功能
class HomeViewController:UIViewController{}
extension HomeViewController:RefreshProtocol {
func scrollToTopRefresh () {
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
}
}
}
答案 1 :(得分:0)
将此添加到按钮的目标
tableView.scrollsToTop = true
tableView.clipsToBounds = true
self.tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)