[SOLVED] touchesBegan to dismiss view

Issue

I have a view in my ViewController that takes up half the screen from bottom to mid-screen. I’m using touchesBegan function to dismiss the view if tapped somewhere else than inside the view, everything works except that when I touch on the 10% of the top on the view, it dismisses when it should not because the touch is still inside the allowed view.

This is my code to present the vc:

            guard let vc = storyboard?.instantiateViewController(withIdentifier: "editvc") as? EditTodoViewController else {return}
                vc.modalPresentationStyle = .custom
                present(vc, animated: true)

and this is my function inside that vc:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if touch != viewForVc {
        EditTodoViewController.textFromCellForTodo = ""
        dismiss(animated: true)
    }
}

Solution

You should check if the touch is not in the view rather than check different

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self.view)
    
    if (!viewForVc.bounds.contains(location)) {
        EditTodoViewController.textFromCellForTodo = ""
        dismiss(animated: true)
    }
}

— Edit—-

If you want to change the frame programmatically before check contains check the code below

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: self.view)
        
        // get the frame of view + 10% height
        let viewFrame = CGRect(origin: viewForVc.frame.origin, size: CGSize(width: viewForVc.frame.size.width, height: viewForVc.frame.size.height * 1.1))
        
        if (!viewFrame.contains(location)) {
            EditTodoViewController.textFromCellForTodo = ""
            dismiss(animated: true)
        }
    }

Answered By – bewithyou

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.