双击 UITabBarItem 滚动到顶部 UITableView
Scroll to top UITableView with double tap on UITabBarItem
我想在他们的列表中模拟 Twitter 或 instagram 的行为:
在 UITabBarItem 中双击时,列表会滚动到顶部。有人知道我该怎么做?
我的 UITabBarController 中有 4 个项目,它们都是列表,所以我想为所有人做这件事。
问题是当我在 mi 列表的推送视图中点击栏项目时,此函数在获取导航的根视图控制器之前调用并检测双击。
希望能解决我的问题
var previousController: UIViewController?
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if previousController == viewController{
if let navVC = viewController as? UINavigationController, vc = navVC.viewControllers.first as? HomeViewController {
vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
print("same")
}
}else{
print("No same")
}
previousController = viewController
}
行为不正确。
当我使用选项卡栏项弹出视图时,根视图位于单元格 0
好的,所以我认为最好的方法是检查 tableView 的 viewController 是否可见,如果不可见,则不要调用 set content offset。
所以:
var previousController: UIViewController?
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if previousController == viewController {
if let navVC = viewController as? UINavigationController, vc = navVC.viewControllers.first as? HomeViewController {
if vc.isViewLoaded() && (vc.view.window != nil) {
// viewController is visible
vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
}
print("same")
}
}else{
print("No same")
}
previousController = viewController
}
Swift 3
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;
}
注意:我使用@Olivier Wilkinson 并展示了如何在需要的地方使用滚动到顶部的扩展。 (已针对集合视图进行测试)
Olivier Wilkinsons 的回答很好,但是向上滚动部分在 Swift 4 中不再起作用,所以替换
vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
和
vc.tableBusinessList.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
一切就绪
我想在他们的列表中模拟 Twitter 或 instagram 的行为:
在 UITabBarItem 中双击时,列表会滚动到顶部。有人知道我该怎么做? 我的 UITabBarController 中有 4 个项目,它们都是列表,所以我想为所有人做这件事。
问题是当我在 mi 列表的推送视图中点击栏项目时,此函数在获取导航的根视图控制器之前调用并检测双击。
希望能解决我的问题
var previousController: UIViewController?
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if previousController == viewController{
if let navVC = viewController as? UINavigationController, vc = navVC.viewControllers.first as? HomeViewController {
vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
print("same")
}
}else{
print("No same")
}
previousController = viewController
}
行为不正确。 当我使用选项卡栏项弹出视图时,根视图位于单元格 0
好的,所以我认为最好的方法是检查 tableView 的 viewController 是否可见,如果不可见,则不要调用 set content offset。
所以:
var previousController: UIViewController?
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if previousController == viewController {
if let navVC = viewController as? UINavigationController, vc = navVC.viewControllers.first as? HomeViewController {
if vc.isViewLoaded() && (vc.view.window != nil) {
// viewController is visible
vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
}
print("same")
}
}else{
print("No same")
}
previousController = viewController
}
Swift 3
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;
}
注意:我使用@Olivier Wilkinson 并展示了如何在需要的地方使用滚动到顶部的扩展。 (已针对集合视图进行测试)
Olivier Wilkinsons 的回答很好,但是向上滚动部分在 Swift 4 中不再起作用,所以替换
vc.tableBusinessList.setContentOffset(CGPointZero, animated: true)
和
vc.tableBusinessList.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
一切就绪