双击酒吧按钮项滚动到顶部
我想滚动到顶部,当我双击任何BarButtonItem
。 我在stackoverflow上看到了很多答案,但他们都没有为我工作。 也许我使用它错了?我在哪里把代码放在AppDelegate
或TableViewControllers
我想特别添加这个功能?双击酒吧按钮项滚动到顶部
无论如何,我使用Swift 2.3和Xcode 8,并希望得到一些帮助。
谢谢。
您是否知道scrollsToTop
?我认为这是你需要的。从iOS版SDK说明在UIScrollView
有关scrollsToTop
属性:
当用户点击状态栏,触摸下方的滚动视图最接近的状态栏会滚动到顶部,但只有当它的
scrollsToTop
财产是YES,其代表不会从shouldScrollViewScrollToTop
返回NO,并且它不在顶部。 //在iPhone上,我们只有在有一个屏幕滚动视图scrollsToTop
== YES时才执行此手势。如果发现多于一个,则不会滚动。
首先设置UITabBarControllerDelegate委托。委托可以通过Storyboard或通过UITabBarController的委托属性代码轻松设置。
myTabBar.delegate = myNewDelegate // Have to conform to UITabBarControllerDelegate protocol
你甚至可以子类的UITabBarController和实施UITabBarControllerDelegate它,因此它可以为自己成为代表。
mySubclassedTabBar.delegate = mySubclassedTabBar
当您有代表时,您可以尝试此方法。
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool
{
guard let tabBarControllers = tabBarController.viewControllers
else
{
// TabBar have no configured controllers
return true
}
if let newIndex = tabBarControllers.indexOf(viewController) where newIndex == tabBarController.selectedIndex
{
// Index won't change so we can scroll
guard let tableViewController = viewController as? UITableViewController // Or any other condition
else
{
// We are not in UITableViewController
return true
}
tableViewController.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
}
return true
}
我把所有的代码放在TableViewController/ViewController中,我想特别添加这个功能吗?或者在一个新的swift文件中,我将连接到我的tabBarViewController? –
最简单的方法是继承UITabBarController(通常是新的swift文件),实现委托协议并在故事板更改tabBarController的类。然后,您可以轻松更改故事板或代码中的委托属性(即在初始化过程中)。 – Adamsor
是啊,我知道,此行中我看到了很多答案,但我总能得到它指向的好听,你可能告诉我如何将其正确写入,使其工作的错误? –
'scrollsToTop'默认是true,但如果你有很多'UIScrollView'或它的子类,你需要为所有需要设置'false'除了需要的视图 –
我唯一的'UIScrollView'是这样的:'override func scrollViewWillBeginDragging scrollView:UIScrollView)self.searchController.searchBar.resignFirstResponder() self.searchController.searchBar.endEditing(true) }'所以我可以轻扫以关闭键盘时,我搜索... –