点击 tabBarItem 以呈现视图控制器
Posted
技术标签:
【中文标题】点击 tabBarItem 以呈现视图控制器【英文标题】:Tap on tabBarItem to Present a View Controller 【发布时间】:2017-06-29 01:37:56 【问题描述】:如果任何 tabBar 项目被按下两次,我在这里有一个自定义命令来滚动到顶部。但现在我想将一个函数添加到特定的 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
感谢您的帮助...
【问题讨论】:
【参考方案1】:试试这个代码。使用点击手势识别器。
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
【讨论】:
我认为你不明白。现在我有一个处理所有 tabBarItems 的函数。但除了该函数之外,我还想告诉索引 3 处的 tabbarItem 在被点击时呈现一个 viewController。【参考方案2】:让我看看我是否正确:您想根据标签栏中选择的项目(第一个、第二个等)做一些事情。如果是这样,我认为您可以根据项目的标签使用它(如果您设置了委托):
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
switch item.tag
case 1:
// your code
default:
// your code
并且不要忘记在标签栏中设置每个项目的标签。
【讨论】:
是的,我知道这一点,但我不能同时使用应该选择和选择功能,我正在寻求帮助以将两者结合起来,因此将同时获得两种功能以上是关于点击 tabBarItem 以呈现视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
2 ViewControllers 以模态方式呈现 UITabBarController
如何在iphone中设置默认选中Tab bar item 1?