I am using this code in appdelegate
IQKeyboardManager.sharedManager().enable = true
IQKeyboardManager.sharedManager().shouldResignOnTouchOutside = true
IQKeyboardManager.sharedManager().touchResignedGestureIgnoreClasses = [UINavigationBar.self,UIControl.self]
assigning touchResignedGestureIgnoreClasses property is allowing me to fire the UIButton event if keyboard is open but it does not dismisses the keyboard simultaneously.
In such specific cases you might required to create your own button class sub-classing UIButton and observe the events inside it. Later then specify UIButtons custom class as your own button which you have created.
I hope this might helps you, if it did't work the follow the another approach below mentioned
Write an extension for UIViewController
And then call this function from your viewcontroller