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

时间:2018-08-07 03:48:50

标签: ios swift uistoryboard uitabbar uitabbaritem

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

我想单击标签栏并显示新的ViewController,但是我无法在storyBoard中这样做

enter image description here enter image description here enter image description here

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

如何单击UITabBarItem?

How to make a UILabel clickable?

@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")
}

1 个答案:

答案 0 :(得分:1)

  

类型'UITabBarItem'的值没有成员'isUserInteractionEnabled',类型'UITabBarItem'的值没有成员'addGestureRecognizer'

由于UITabBarItem继承自UIBarItem,而UIBarItem继承自NSObject,而不是UIView

  

要启用/禁用UITabBarItem,可以使用isEnabled属性

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

  

要捕获UITabBarItem的动作,可以在UITabBar的委托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