0

I am using SWRevealViewController for side menu and IQKeyboardManagerSwift for keyboard.

When I am editing content in textfield and tries to open menu, the keyboard should automatically hide but I can't manage to do so.

How should this be done?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Gaurav
  • 97
  • 3
  • 9

2 Answers2

0
  1. Capture the event of the opening menu. You can do this with SWRevealViewcontroller Delegate or by simply adding an @IBAction to the menu button.

  2. In this method call .resignFirstResponder() for the element that needs the keyboard (like a textField):

    textField.resignFirstResponder()

Of course you can call this function in every element that can have a keyboard to be sure to call the right one.

M. Kremer
  • 679
  • 6
  • 24
0

Since UIBarButtonItem doesn’t inherit from UIView or expose the item’s underlying view, this isn’t as easy as adding a gesture recognizer.

One plausible solution would be to [Step 1] define a custom view for the Side Menu Icon [Step 2]add the gesture to hide keyboard to it.

//gesture:tap anywehere to dismiss the keyboard
    let tap = UITapGestureRecognizer(target:self.view,action:#selector(UIView.endEditing))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)

    let customButton = UIButton(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))
    customButton.setImage(UIImage(named: "menu"), for: .normal)
    //hide keyboard gesture(tap gesture)
    customButton.addGestureRecognizer(tap)
    customButton.isUserInteractionEnabled = true

    if self.revealViewController() != nil {
        customButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
    self.navigationItem.leftBarButtonItem?.customView = customButton

Please accept as answer if it works.(Worked for me)