0

I add a tab bar to view controller manually, this is not a tabBarController, because I only want the tab bar UI, not the tab bar function

I want to click on tab bar and show new ViewController, but I cannot do that in storyBoard

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

If I use this method, xcode prompt me Value of type 'UITabBarItem' has no member 'isUserInteractionEnabled' and Value of type 'UITabBarItem' has no member 'addGestureRecognizer'

How can I click a 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")
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
CL So
  • 3,647
  • 10
  • 51
  • 95

1 Answers1

1

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

Because of UITabBarItem inherits from UIBarItem, that inherits from NSObject, not 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

and got the item's position via UITabBarItem.tag https://developer.apple.com/documentation/uikit/uibaritem/1616419-tag?changes=_2

Quoc Nguyen
  • 2,839
  • 6
  • 23
  • 28