gpt4 book ai didi

swift - 按下两次时,Tab Bar 滚动到 UIViewController 的顶部

转载 作者:可可西里 更新时间:2023-11-01 01:56:10 25 4
gpt4 key购买 nike

我试图让我的 UITabBarController 在按下两次时滚动到页面顶部。我已经尝试了几次,但没有成功。截至目前,我只有一个链接到代码的 Storyboard上的 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
}
}

最佳答案

例如:

   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
}
}

关于swift - 按下两次时,Tab Bar 滚动到 UIViewController 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com