我正在尝试使我的UITabBarController
按下两次时滚动到页面顶部。我已经尝试过几次,以使其无法正常工作。到目前为止,在故事板上的UITabBarController
只有一个与代码链接的类。我是否也应该链接UITabBar
?这是到目前为止我尝试过的代码。
import UIKit
class TabViewController: UITabBarController, UITabBarControllerDelegate {
var pressedCount: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func unwindToMain(segue: UIStoryboardSegue) {}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}
// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller")
}
func tabBarController(_ TabViewController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let viewControllers = viewControllers else { return false }
if viewController == viewControllers[selectedIndex] {
if let nav = viewController as? UINavigationController {
guard let topController = nav.viewControllers.last else { return true }
if !topController.isScrolledToTop {
topController.scrollToTop()
return false
} else {
nav.popViewController(animated: true)
}
return true
}
}
return true
}
}
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: view)
}
var isScrolledToTop: Bool {
if self is UITableViewController {
return (self as! UITableViewController).tableView.contentOffset.y == 0
}
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
答案 0 :(得分:1)
例如:
var pressedCount: Int = 0
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
pressedCount += 1
if pressedCount > 1 {
scrollToTop(view: self.view)
} else {
//do something for first press
}
}
答案 1 :(得分:0)
自定义UITabBar,然后使用通知时间使外界响应!或记录通过UITabBarController代理程序的点击次数,然后通知外界做出响应!