Reputation: 139
i m trying to present a view controller as popoverviewcontroller using modal style of type .pagesheet. Here i m trying to add a tap gesture recognizer to dismiss this popoverviewcontroller on clicking ouside its view. But it is not detecting tap in iOS 9. Here is the code below of gesture recognizer
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let recog : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action:#selector(HandleTap))
recog.numberOfTapsRequired = 1
recog.numberOfTouchesRequired = 1
recog.cancelsTouchesInView = false
recog.delegate = self
self.view.window?.addGestureRecognizer(recog)
}
func HandleTap(sender:UITapGestureRecognizer) -> Void
{
if(sender.state == UIGestureRecognizerState.Ended)
{
var location : CGPoint = sender.locationInView(self.presentingViewController?.view)
//var location : CGPoint = sender.locationInView(self.view?.window)
if(!(self.view.pointInside(self.view.convertPoint(location, toView: self.view?.window), withEvent: nil)))
{
self.view.window?.removeGestureRecognizer(sender)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
Upvotes: 3
Views: 1070
Reputation: 27428
Set userinteractionenable
to true
on view
on which you handle the tap. and second thing why you are doing your stuff in viewDidAppear
? You should use viewDidLoad
to add gesture recognizer.
Update :
self.view.window?.userInteractionEnabled = true
if you are using navigation controller then
self.navigationController?.view.window?.userInteractionEnabled = true
hope thiw will help :)
Upvotes: 1