gpt4 book ai didi

ios - 点击 tabBarItem 以显示 View Controller

转载 作者:行者123 更新时间:2023-11-28 15:30:06 25 4
gpt4 key购买 nike

如果任何 tabBar 项目被按下两次,我在这里有一个自定义命令 scrollToTop。但现在我想向特定的 tabBaritem 添加一个功能(标签 3 让我们说......)

我如何结合我现在拥有的:

import UIKit

class TabBarController: UITabBarController,UITabBarControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
delegate = self
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBarController(_ tabBarController: 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
}
}

有了这个(这样我就可以对特定的 tabBarItem 进行特定的操作):

func tabBarController(_ tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if tabBarController.selectedIndex == 0 {
// present your first view controller
}
else if tabBarController.selectedIndex == 1 {
// present your second view controller
}
else if tabBarController.selectedIndex == 2 {
// present your third view controller
}

}

谢谢你的帮助...

最佳答案

试试这段代码。使用点击手势识别器。

class MyTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer()
tap.numberOfTapsRequired = 2
tap.addTarget(self, action: #selector(MyTabBarController.twiceTapped(sender:)))
tabBar.addGestureRecognizer(tap)
}

func twiceTapped(sender: UITapGestureRecognizer) {
// handle here
}
}

关于ios - 点击 tabBarItem 以显示 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815024/

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