clydececiljohn
clydececiljohn

Reputation: 27

hide the keyboard on the button click

I have used the IQKeyboardManager in my project .And i have set in the app delegate.

But i need to hide the iqkeyboardmanager while clicking on the button.How to do it.

currently my code in app delegate:-

   IQKeyboardManager.sharedManager().enable = true
   IQKeyboardManager.sharedManager().enableAutoToolbar = false
   IQKeyboardManager.sharedManager().shouldShowToolbarPlaceholder = false
   IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true

I have used tableview.So in the tableviewcell i have a button. So while clicking on the button i need to hidden the keyboard .How to hide it?

Upvotes: 0

Views: 1013

Answers (2)

Parth Patel
Parth Patel

Reputation: 919

If you are using Textfield then use this method for hide keyboard

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
}

Otherwise, put this code in your button click method

[[IQKeyboardManager sharedManager] resignFirstResponder];

Here I found the best way to hide Keyboard on any screen.

  1. Create Extension for UIResponder

    extension UIResponder {
    
         private static weak var _currentFirstResponder: UIResponder?
    
         static var currentFirstResponder: UIResponder? {
             _currentFirstResponder = nil
             UIApplication.shared.sendAction(#selector(UIResponder.findFirstResponder(_:)), to: nil, from: nil, for: nil)
    
             return _currentFirstResponder
         }
    
         @objc func findFirstResponder(_ sender: Any) {
             UIResponder._currentFirstResponder = self
         }
    
    }
    
  2. How to use it

    UIResponder.currentFirstResponder?.resignFirstResponder()
    

Please prefer this link: https://github.com/hackiftekhar/IQKeyboardManager/issues/659

Upvotes: 2

nitin.agam
nitin.agam

Reputation: 2142

As you said in your question, First of all, IQKeyboardManager is not a keyboard which you can hide or show. It is a library to manage scrolling and keyboard on views.

Now, you can hide the keyboard from tableCell on button click: UIApplication.shared.sendAction(#selector(UIResponder.resign‌FirstResponder), to: nil, from: nil, for: nil)

Upvotes: 0

Related Questions