Complete answer for Swift 4
Step 1: create an outlet for the view
@IBOutlet weak var rightViewOutlet: UIView!
Step 2: define a tap gesture
var tapGesture = UITapGestureRecognizer()
Step 3: create ObjC function (called when view tapped)
@objc func rightViewTapped(_ recognizer: UIGestureRecognizer) {
print("Right button is tapped")
}
Step 4: add the following within viewDidLoad()
let rightTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.rightViewTapped(_:)))
rightViewOutlet.addGestureRecognizer(rightTap)