4

I'm writing my first app, using Swift, and I need a popover or modal view that can be dismissed by touching anywhere on the screen.

I'm writing an IOU app and am currently working on the view where the user enters the lenders and how much they lend. Obviously each lender needs to have a unique name, and I'd like a popover or modal view to appear whenever the user tries to enter the same name twice asking them to change the name. To lessen the irritation factor, I'd like to make it so that the user can tap anywhere on the screen to dismiss the warning, rather than on a specific button.

I found this answer: Detect touch globally, and I think it might work for me, but I know nothing of Objective-C, just Swift, and couldn't understand enough to know what to do.

Community
  • 1
  • 1
7OO Tnega Terces
  • 147
  • 1
  • 1
  • 7

8 Answers8

9

Dismissing a modal view turns out to be surprisingly easy. All you need to do is call dismissViewControllerAnimated(true, completion: nil). Thus, to do what I wanted, all I needed to do was this:

override func touchesEnded(touches: NSSet, withEvent event: UIEvent)
{
  dismissViewControllerAnimated(true, completion: nil)
  super.touchesEnded(touches, withEvent: event)
}
7OO Tnega Terces
  • 147
  • 1
  • 1
  • 7
7

Swift 3.0

// Add this to your UIViewController class

     override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

           //Do thing here
        }
uplearned.com
  • 3,393
  • 5
  • 44
  • 59
3

I personally do not know how to dismiss popover's since I haven't used them, but I can fix one of your problems. You say that you want to dismiss a popover or modal when the user touches anywhere on the screen. Here is a function that triggers when the screen is touched in any location, you can read more about it in the apple documents.

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    }
Guled
  • 659
  • 1
  • 5
  • 18
3

I found a great way of doing this by adding a UIGestureRecognizer.

In AppDelegate conform to UIGestureRecognizerDelegate, by adding it to your

class AppDelegate: UIResponder, UIApplicationDelegate, UIGestureRecognizerDelegate {

...

Add tapGestures

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // add tapGestures to entire application
        let tapGesture = UITapGestureRecognizer(target: self, action: nil)
        tapGesture.delegate = self
        window?.addGestureRecognizer(tapGesture)

        return true
    }

add the gestureRecognizer protocol function:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    // User tapped on screen, do something

    return false
}
Macness
  • 1,226
  • 2
  • 13
  • 24
1

I'd like to make it so that the user can tap anywhere on the screen to dismiss the warning, rather than on a specific button.

This is probably a very, very bad idea and you might encounter gesture collisions with your text fields or other elements in the modal view, or you might upset the user whenever they dismiss the modal by accident, but hey, whatever rocks your boat.

Get the view of the topmost view controller in your modal view. If it's a UINavigationController that contains YourModalViewController, you would do this in your modal's viewDidLoad:

if let navController = self.navigationController {
    navController.view.addGestureRecognizer(UITapGestureRecognizer(...))
}

And then dismiss your modal from inside the gesture recognizer's action method.

Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
1

Swift 4.2 :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // Touch began
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        // Touch end
    }
Sunil Targe
  • 7,251
  • 5
  • 49
  • 80
1

I found a very easy solution. I am working on Xcode 13 with Swift 5. Use this :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  <nameOfTextField>.endEditing(true)
}

I used this to register touch and dismiss keyboard when tapped anywhere on the screen. <nameOfTextField> is the text field that triggered the keyboard in the first place.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-1

I am working on an application. Where we need to maintain a session of 10-15 minutes. For this, I was looking for a user's touch event.

I already have the base controller in the app. So I update the session on viewwillappear. I will not need any touch events.

Urvish Modi
  • 1,118
  • 10
  • 11