0

I have a viewController EditMessage which has two UITextFields (UITextView) which use the keyboard and they work great. This part is basic standard stuff. When the keyboard is displayed, I register a tag gesture for the entire view, so that if the user clicks anywhere else, I dismiss the keyboard:

 self.view.addGestureRecognizer(UITapGestureRecognizer(target: self,
         action: #selector(dismissKeyboard)))

In dismissKeyboard, this all works fine:

@objc func dismissKeyboard(sender: Any) {
    self.view.endEditing(true)
}

However, I have a menu button(thumbnail image) implemented as a child view controller (UIViewController) on the same EditMessage view, which hijacks the screen via UIApplication.shared.keyWindow() to display an overlay and menu on the bottom of the screen. Built using the model/code from Brian Voong's YouTube channel to replicate a YouTube style slide in menu from the bottom. However, the keyboard is in the way. Since the child is a different view controller "endEditing" doesn't work (or maybe I am referencing the wrong view?).

class ButtonPickerController : UIViewController, 
UIGestureRecognizerDelegate, UINavigationControllerDelegate {

var maxSize = CGFloat(60)

let thumbnail: UIImageView = {
    let thumbnail = UIImageView()
    thumbnail.backgroundColor = UIColor.lightGray
    return thumbnail
}()

override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.buttonTapped(sender:)))
    tap.delegate = self
    view.addGestureRecognizer(tap)
    //view.backgroundColor = .yellow
    view.contentMode = .scaleAspectFit
    thumbnail.frame = CGRect(x: 0, y: 0, width: self.maxSize, height: self.maxSize)
    setupSubviews()
}

Can someone point me in a good direction? This is my first question so hopefully I am asking properly.

Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
David J
  • 1,018
  • 1
  • 9
  • 14
  • self.view.endEditing(true) should work – Manish Mahajan Apr 27 '18 at 12:15
  • use default method name **resignFirstResponder**. search for it. – MRizwan33 Apr 27 '18 at 12:16
  • Possible duplicate of [resignFirstResponder vs. endEditing for Keyboard Dismissal](https://stackoverflow.com/questions/29882775/resignfirstresponder-vs-endediting-for-keyboard-dismissal) – MRizwan33 Apr 27 '18 at 12:18
  • 1
    Possible duplicate of [How to dismiss keyboard iOS programmatically](https://stackoverflow.com/questions/18755410/how-to-dismiss-keyboard-ios-programmatically) – Kuldeep Apr 27 '18 at 12:19

2 Answers2

1

I figured it out in the end. Thank you for the help. In my child view controller I did used the following statement when the button was tapped:

@objc func buttonTapped(sender: UITapGestureRecognizer? = nil) {
    self.parent!.view.endEditing(true)
}
David J
  • 1,018
  • 1
  • 9
  • 14
0

First, its not a good way to present overlays as UIViewController.

But a solution good be, to give the second viewcontroller a reference to the first one before viewDidLoad is called. Do you use Segues ? So in prepare would be the right place. In the second viewcontroller you create a property for the first one and then use this property as target when you create the UITapGestureRecognizer.

Another way is using a protocol and delegation.

thorb65
  • 2,696
  • 2
  • 27
  • 37
  • I received the idea of using an overlay as UIViewController from the Apple Documentation. They recommend doing this when you have a collection of subviews you want to present, so that the code is cleaner. – David J May 05 '18 at 14:07