I have a simple code to add tapgestureRecognizer to dismis ViewController when tap in screen, but it not working only in iPhone X 12.1.
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(closeSharing(_:))))
Note: magicaly, when i add tap gesturecognizer to a new view that constraint equal to view that also not working, but if i constraint the new view not equal to view, that working. Does any one know why?.
You might need to check a couple of things here:
First step make sure that your main view is not covered for example of another top view which will break your tap gesture, so add the gesture to your top view.
Second step Make sure your view that will have the gesture should have the property view.isUserInteractionEnabled = true
, otherwise the gesture will not work.
Third step Make sure your view appears when your testing, you might have a problem with constraints, so the view is out of screen bounds, so try giving the view a backgroundColor = .red
to see if it exists or not, or you can use View Debugger from xCode.
Example of a working gesture:
let viewToDismiss = UIView(frame:UIScreen.main.bounds)
viewToDismiss.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(closeSharing(_:)))
tap.numberOfTapsRequired = 1
viewToDismiss.addGestureRecognizer(tap)