【问题标题】:Tap TabBarItem to scrollToTop点击 TabBarItem 滚动顶部
【发布时间】:2017-07-24 22:28:47
【问题描述】:

怎么做?

我尝试了所有关于堆栈溢出的方法,但没有任何效果。我想要一个可以像 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)
        }
    }

【问题讨论】:

    标签: ios swift uitableview uitabbarcontroller scrolltop


    【解决方案1】:

    在您的按钮操作中,调用tableView.scrollToRow(IndexPath(row: 0, section:0), at scrollPosition: .top, animated: true)

    【讨论】:

    • 我必须将他的行修复为:tableView.scrollToRow(NSIndexPath(row: 0, section:0), at, scrollPosition: .top, animated: true),我收到错误use of unresolved identifier 'at'
    • 是的,您在“at”之后添加了一个额外的逗号。我的代码在 swift 3 中,所以如果您使用的是旧版本,则需要更改它。
    【解决方案2】:

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

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

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

    然后在tabbarbutton这样调用函数,

    tableView.scrollToTop(animated: true)
    

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

    【讨论】:

    • 当我向下滚动时我想要的不起作用我按下标签栏按钮,它会向上滚动
    • 我会调查它,但你说的是状态栏而不是标签栏,对吧?
    • 我在谈论标签栏。我希望当我在我的 tableviewcontroller 中向下滚动时,我会按下当前选中的标签栏按钮,它会滚动到顶部,当你点击状态栏时会发生什么,但如果你按下当前的 barbuttonitem...你明白了吗?
    • 很酷。我将在即将到来的周末尝试一下,因为我刚回到军队。
    • 没有@K-2SO 它没有
    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 2013-03-13
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多