IQKeyboardManager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // IQKeyboardManager.h
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-16 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import "IQKeyboardManagerConstants.h"
  24. #import "IQUIView+IQKeyboardToolbar.h"
  25. #import "IQPreviousNextView.h"
  26. #import "IQUIViewController+Additions.h"
  27. #import "IQKeyboardReturnKeyHandler.h"
  28. #import "IQTextView.h"
  29. #import "IQToolbar.h"
  30. #import "IQUIScrollView+Additions.h"
  31. #import "IQUITextFieldView+Additions.h"
  32. #import "IQBarButtonItem.h"
  33. #import "IQTitleBarButtonItem.h"
  34. #import "IQUIView+Hierarchy.h"
  35. #import <CoreGraphics/CGBase.h>
  36. #import <Foundation/NSObject.h>
  37. #import <Foundation/NSObjCRuntime.h>
  38. #import <Foundation/NSSet.h>
  39. #import <UIKit/UITextInputTraits.h>
  40. @class UIFont, UIColor, UITapGestureRecognizer, UIView, UIImage;
  41. @class NSString;
  42. ///---------------------
  43. /// @name IQToolbar tags
  44. ///---------------------
  45. /**
  46. Default tag for toolbar with Done button -1002.
  47. */
  48. extern NSInteger const kIQDoneButtonToolbarTag;
  49. /**
  50. Default tag for toolbar with Previous/Next buttons -1005.
  51. */
  52. extern NSInteger const kIQPreviousNextButtonToolbarTag;
  53. /**
  54. Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more. A generic version of KeyboardManagement. https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
  55. */
  56. @interface IQKeyboardManager : NSObject
  57. ///--------------------------
  58. /// @name UIKeyboard handling
  59. ///--------------------------
  60. /**
  61. Returns the default singleton instance. You are not allowed to create your own instances of this class.
  62. */
  63. + (nonnull instancetype)sharedManager;
  64. /**
  65. Enable/disable managing distance between keyboard and textField. Default is YES(Enabled when class loads in `+(void)load` method).
  66. */
  67. @property(nonatomic, assign, getter = isEnabled) BOOL enable;
  68. /**
  69. To set keyboard distance from textField. can't be less than zero. Default is 10.0.
  70. */
  71. @property(nonatomic, assign) CGFloat keyboardDistanceFromTextField;
  72. /**
  73. Refreshes textField/textView position if any external changes is explicitly made by user.
  74. */
  75. - (void)reloadLayoutIfNeeded;
  76. /**
  77. Boolean to know if keyboard is showing.
  78. */
  79. @property(nonatomic, assign, readonly, getter = isKeyboardShowing) BOOL keyboardShowing;
  80. /**
  81. moved distance to the top used to maintain distance between keyboard and textField. Most of the time this will be a positive value.
  82. */
  83. @property(nonatomic, assign, readonly) CGFloat movedDistance;
  84. ///-------------------------
  85. /// @name IQToolbar handling
  86. ///-------------------------
  87. /**
  88. Automatic add IQToolbar functionality. Default is YES.
  89. */
  90. @property(nonatomic, assign, getter = isEnableAutoToolbar) BOOL enableAutoToolbar;
  91. /**
  92. IQAutoToolbarBySubviews: Creates Toolbar according to subview's hirarchy of Textfield's in view.
  93. IQAutoToolbarByTag: Creates Toolbar according to tag property of TextField's.
  94. IQAutoToolbarByPosition: Creates Toolbar according to the y,x position of textField in it's superview coordinate.
  95. Default is IQAutoToolbarBySubviews.
  96. */
  97. @property(nonatomic, assign) IQAutoToolbarManageBehaviour toolbarManageBehaviour;
  98. /**
  99. If YES, then uses textField's tintColor property for IQToolbar, otherwise tint color is black. Default is NO.
  100. */
  101. @property(nonatomic, assign) BOOL shouldToolbarUsesTextFieldTintColor;
  102. /**
  103. This is used for toolbar.tintColor when textfield.keyboardAppearance is UIKeyboardAppearanceDefault. If shouldToolbarUsesTextFieldTintColor is YES then this property is ignored. Default is nil and uses black color.
  104. */
  105. @property(nullable, nonatomic, strong) UIColor *toolbarTintColor;
  106. /**
  107. This is used for toolbar.barTintColor. Default is nil and uses white color.
  108. */
  109. @property(nullable, nonatomic, strong) UIColor *toolbarBarTintColor;
  110. /**
  111. IQPreviousNextDisplayModeDefault: Show NextPrevious when there are more than 1 textField otherwise hide.
  112. IQPreviousNextDisplayModeAlwaysHide: Do not show NextPrevious buttons in any case.
  113. IQPreviousNextDisplayModeAlwaysShow: Always show nextPrevious buttons, if there are more than 1 textField then both buttons will be visible but will be shown as disabled.
  114. */
  115. @property(nonatomic, assign) IQPreviousNextDisplayMode previousNextDisplayMode;
  116. /**
  117. Toolbar previous/next/done button icon, If nothing is provided then check toolbarDoneBarButtonItemText to draw done button.
  118. */
  119. @property(nullable, nonatomic, strong) UIImage *toolbarPreviousBarButtonItemImage;
  120. @property(nullable, nonatomic, strong) UIImage *toolbarNextBarButtonItemImage;
  121. @property(nullable, nonatomic, strong) UIImage *toolbarDoneBarButtonItemImage;
  122. /**
  123. Toolbar previous/next/done button text, If nothing is provided then system default 'UIBarButtonSystemItemDone' will be used.
  124. */
  125. @property(nullable, nonatomic, strong) NSString *toolbarPreviousBarButtonItemText;
  126. @property(nullable, nonatomic, strong) NSString *toolbarNextBarButtonItemText;
  127. @property(nullable, nonatomic, strong) NSString *toolbarDoneBarButtonItemText;
  128. /**
  129. If YES, then it add the textField's placeholder text on IQToolbar. Default is YES.
  130. */
  131. @property(nonatomic, assign) BOOL shouldShowToolbarPlaceholder;
  132. /**
  133. Placeholder Font. Default is nil.
  134. */
  135. @property(nullable, nonatomic, strong) UIFont *placeholderFont;
  136. /**
  137. Placeholder Color. Default is nil. Which means lightGray
  138. */
  139. @property(nullable, nonatomic, strong) UIColor *placeholderColor;
  140. /**
  141. Placeholder Button Color when it's treated as button. Default is nil. Which means iOS Blue for light toolbar and Yellow for dark toolbar
  142. */
  143. @property(nullable, nonatomic, strong) UIColor *placeholderButtonColor;
  144. /**
  145. Reload all toolbar buttons on the fly.
  146. */
  147. - (void)reloadInputViews;
  148. ///---------------------------------------
  149. /// @name UIKeyboard appearance overriding
  150. ///---------------------------------------
  151. /**
  152. Override the keyboardAppearance for all textField/textView. Default is NO.
  153. */
  154. @property(nonatomic, assign) BOOL overrideKeyboardAppearance;
  155. /**
  156. If overrideKeyboardAppearance is YES, then all the textField keyboardAppearance is set using this property.
  157. */
  158. @property(nonatomic, assign) UIKeyboardAppearance keyboardAppearance;
  159. ///-----------------------------------------------------------
  160. /// @name UITextField/UITextView Next/Previous/Resign handling
  161. ///-----------------------------------------------------------
  162. /**
  163. Resigns Keyboard on touching outside of UITextField/View. Default is NO.
  164. */
  165. @property(nonatomic, assign) BOOL shouldResignOnTouchOutside;
  166. /** TapGesture to resign keyboard on view's touch. It's a readonly property and exposed only for adding/removing dependencies if your added gesture does have collision with this one */
  167. @property(nonnull, nonatomic, strong, readonly) UITapGestureRecognizer *resignFirstResponderGesture;
  168. /**
  169. Resigns currently first responder field.
  170. */
  171. - (BOOL)resignFirstResponder;
  172. /**
  173. Returns YES if can navigate to previous responder textField/textView, otherwise NO.
  174. */
  175. @property (nonatomic, readonly) BOOL canGoPrevious;
  176. /**
  177. Returns YES if can navigate to next responder textField/textView, otherwise NO.
  178. */
  179. @property (nonatomic, readonly) BOOL canGoNext;
  180. /**
  181. Navigate to previous responder textField/textView.
  182. */
  183. - (BOOL)goPrevious;
  184. /**
  185. Navigate to next responder textField/textView.
  186. */
  187. - (BOOL)goNext;
  188. ///-----------------------
  189. /// @name UISound handling
  190. ///-----------------------
  191. /**
  192. If YES, then it plays inputClick sound on next/previous/done click. Default is YES.
  193. */
  194. @property(nonatomic, assign) BOOL shouldPlayInputClicks;
  195. ///---------------------------
  196. /// @name UIAnimation handling
  197. ///---------------------------
  198. /**
  199. If YES, then calls 'setNeedsLayout' and 'layoutIfNeeded' on any frame update of to viewController's view.
  200. */
  201. @property(nonatomic, assign) BOOL layoutIfNeededOnUpdate;
  202. ///---------------------------------------------
  203. /// @name Class Level enabling/disabling methods
  204. ///---------------------------------------------
  205. /**
  206. Disable distance handling within the scope of disabled distance handling viewControllers classes. Within this scope, 'enabled' property is ignored. Class should be kind of UIViewController. Default is [UITableViewController, UIAlertController, _UIAlertControllerTextFieldViewController].
  207. */
  208. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *disabledDistanceHandlingClasses;
  209. /**
  210. Enable distance handling within the scope of enabled distance handling viewControllers classes. Within this scope, 'enabled' property is ignored. Class should be kind of UIViewController. Default is [].
  211. */
  212. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *enabledDistanceHandlingClasses;
  213. /**
  214. Disable automatic toolbar creation within the scope of disabled toolbar viewControllers classes. Within this scope, 'enableAutoToolbar' property is ignored. Class should be kind of UIViewController. Default is [UIAlertController, _UIAlertControllerTextFieldViewController].
  215. */
  216. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *disabledToolbarClasses;
  217. /**
  218. Enable automatic toolbar creation within the scope of enabled toolbar viewControllers classes. Within this scope, 'enableAutoToolbar' property is ignored. Class should be kind of UIViewController. Default is [].
  219. */
  220. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *enabledToolbarClasses;
  221. /**
  222. Allowed subclasses of UIView to add all inner textField, this will allow to navigate between textField contains in different superview. Class should be kind of UIView. Default is [UITableView, UICollectionView, IQPreviousNextView].
  223. */
  224. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *toolbarPreviousNextAllowedClasses;
  225. /**
  226. Disabled classes to ignore 'shouldResignOnTouchOutside' property, Class should be kind of UIViewController. Default is [UIAlertController, UIAlertControllerTextFieldViewController]
  227. */
  228. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *disabledTouchResignedClasses;
  229. /**
  230. Enabled classes to forcefully enable 'shouldResignOnTouchOutsite' property. Class should be kind of UIViewController. Default is [].
  231. */
  232. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *enabledTouchResignedClasses;
  233. /**
  234. if shouldResignOnTouchOutside is enabled then you can customise the behaviour to not recognise gesture touches on some specific view subclasses. Class should be kind of UIView. Default is [UIControl, UINavigationBar]
  235. */
  236. @property(nonatomic, strong, nonnull, readonly) NSMutableSet<Class> *touchResignedGestureIgnoreClasses;
  237. ///-------------------------------------------
  238. /// @name Third Party Library support
  239. /// Add TextField/TextView Notifications customised NSNotifications. For example while using YYTextView https://github.com/ibireme/YYText
  240. ///-------------------------------------------
  241. /**
  242. Add/Remove customised Notification for third party customised TextField/TextView. Please be aware that the NSNotification object must be idential to UITextField/UITextView NSNotification objects and customised TextField/TextView support must be idential to UITextField/UITextView.
  243. @param didBeginEditingNotificationName This should be identical to UITextViewTextDidBeginEditingNotification
  244. @param didEndEditingNotificationName This should be identical to UITextViewTextDidEndEditingNotification
  245. */
  246. -(void)registerTextFieldViewClass:(nonnull Class)aClass
  247. didBeginEditingNotificationName:(nonnull NSString *)didBeginEditingNotificationName
  248. didEndEditingNotificationName:(nonnull NSString *)didEndEditingNotificationName;
  249. -(void)unregisterTextFieldViewClass:(nonnull Class)aClass
  250. didBeginEditingNotificationName:(nonnull NSString *)didBeginEditingNotificationName
  251. didEndEditingNotificationName:(nonnull NSString *)didEndEditingNotificationName;
  252. ///----------------------------------------
  253. /// @name Debugging & Developer options
  254. ///----------------------------------------
  255. @property(nonatomic, assign) BOOL enableDebugging;
  256. /**
  257. @warning Use these methods to completely enable/disable notifications registered by library internally. Please keep in mind that library is totally dependent on NSNotification of UITextField, UITextField, Keyboard etc. If you do unregisterAllNotifications then library will not work at all. You should only use below methods if you want to completedly disable all library functions. You should use below methods at your own risk.
  258. */
  259. -(void)registerAllNotifications;
  260. -(void)unregisterAllNotifications;
  261. ///----------------------------------------
  262. /// @name Must not be used for subclassing.
  263. ///----------------------------------------
  264. /**
  265. Unavailable. Please use sharedManager method
  266. */
  267. -(nonnull instancetype)init NS_UNAVAILABLE;
  268. /**
  269. Unavailable. Please use sharedManager method
  270. */
  271. + (nonnull instancetype)new NS_UNAVAILABLE;
  272. @end