如何点击 UITabBarItem 并显示新的 ViewController?

How to click UITabBarItem and show new ViewController?

我手动添加了一个标签栏来查看控制器,这不是tabBarController,因为我只想要标签栏UI,而不是标签栏功能

我想单击标签栏并显示新的 ViewController,但我无法在 storyBoard

中执行此操作

如果我使用这个方法,xcode提示我Value of type 'UITabBarItem' has no member 'isUserInteractionEnabled'Value of type 'UITabBarItem' has no member 'addGestureRecognizer'

如何单击 UITabBarItem?

@IBOutlet weak var setting: UITabBarItem!
@IBOutlet weak var activity: UITabBarItem!
@IBOutlet weak var profile: UITabBarItem!
@IBOutlet weak var connect: UITabBarItem!
@IBOutlet weak var scanner: UITabBarItem!

override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction))
    setting.isUserInteractionEnabled = true
    setting.addGestureRecognizer(tap)

    activity.isUserInteractionEnabled = true
    activity.addGestureRecognizer(tap)

    profile.isUserInteractionEnabled = true
    profile.addGestureRecognizer(tap)

    connect.isUserInteractionEnabled = true
    connect.addGestureRecognizer(tap)

    scanner.isUserInteractionEnabled = true
    scanner.addGestureRecognizer(tap)
}
@objc func tapFunction(sender:UITapGestureRecognizer) {

    print("tap working")
}

Value of type 'UITabBarItem' has no member 'isUserInteractionEnabled' and Value of type 'UITabBarItem' has no member 'addGestureRecognizer'

因为UITabBarItem继承自UIBarItem,UIBarItem继承自NSObject,而不是UIView

For enable/disable the UITabBarItem, you can using the isEnabled property

https://developer.apple.com/documentation/uikit/uibaritem/1616418-isenabled?changes=_2

For catch the action of UITabBarItem, you can catch it in the UITabBar's delegate tabbar:didselectitem

https://developer.apple.com/documentation/uikit/uitabbardelegate/1623463-tabbar?changes=_2

并通过 UITabBarItem.tag 获得了项目的位置 https://developer.apple.com/documentation/uikit/uibaritem/1616419-tag?changes=_2