override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first if touch?.view != self.yourView { self.dismiss(animated: true, completion: nil) } }