IQKeyboardManager.m 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  1. //
  2. // IQKeyboardManager.m
  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 "IQKeyboardManager.h"
  24. #import "IQUIView+Hierarchy.h"
  25. #import "IQUIView+IQKeyboardToolbar.h"
  26. #import "IQNSArray+Sort.h"
  27. #import "IQKeyboardManagerConstantsInternal.h"
  28. #import "IQUIScrollView+Additions.h"
  29. #import "IQUITextFieldView+Additions.h"
  30. #import "IQUIViewController+Additions.h"
  31. #import "IQPreviousNextView.h"
  32. #import <QuartzCore/CABase.h>
  33. #import <objc/runtime.h>
  34. #import <UIKit/UIAlertController.h>
  35. #import <UIKit/UISearchBar.h>
  36. #import <UIKit/UIScreen.h>
  37. #import <UIKit/UINavigationBar.h>
  38. #import <UIKit/UITapGestureRecognizer.h>
  39. #import <UIKit/UITextField.h>
  40. #import <UIKit/UITextView.h>
  41. #import <UIKit/UITableViewController.h>
  42. #import <UIKit/UICollectionViewController.h>
  43. #import <UIKit/UICollectionViewCell.h>
  44. #import <UIKit/UICollectionViewLayout.h>
  45. #import <UIKit/UINavigationController.h>
  46. #import <UIKit/UITouch.h>
  47. #import <UIKit/UIWindow.h>
  48. #import <UIKit/UIStackView.h>
  49. #import <UIKit/NSLayoutConstraint.h>
  50. #import <UIKit/UIStackView.h>
  51. #import <UIKit/UIAccessibility.h>
  52. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  53. #import <UIKit/UIWindowScene.h>
  54. #import <UIKit/UIStatusBarManager.h>
  55. #endif
  56. NSInteger const kIQDoneButtonToolbarTag = -1002;
  57. NSInteger const kIQPreviousNextButtonToolbarTag = -1005;
  58. #define kIQCGPointInvalid CGPointMake(CGFLOAT_MAX, CGFLOAT_MAX)
  59. typedef void (^SizeBlock)(CGSize size);
  60. @interface IQKeyboardManager()<UIGestureRecognizerDelegate>
  61. /*******************************************/
  62. /** used to adjust contentInset of UITextView. */
  63. @property(nonatomic, assign) UIEdgeInsets startingTextViewContentInsets;
  64. /** used to adjust scrollIndicatorInsets of UITextView. */
  65. @property(nonatomic, assign) UIEdgeInsets startingTextViewScrollIndicatorInsets;
  66. /** used with textView to detect a textFieldView contentInset is changed or not. (Bug ID: #92)*/
  67. @property(nonatomic, assign) BOOL isTextViewContentInsetChanged;
  68. /*******************************************/
  69. /** To save UITextField/UITextView object voa textField/textView notifications. */
  70. @property(nullable, nonatomic, weak) UIView *textFieldView;
  71. /** To save rootViewController.view.frame.origin. */
  72. @property(nonatomic, assign) CGPoint topViewBeginOrigin;
  73. /** To save rootViewController */
  74. @property(nullable, nonatomic, weak) UIViewController *rootViewController;
  75. /** To overcome with popGestureRecognizer issue Bug ID: #1361 */
  76. @property(nullable, nonatomic, weak) UIViewController *rootViewControllerWhilePopGestureRecognizerActive;
  77. @property(nonatomic, assign) CGPoint topViewBeginOriginWhilePopGestureRecognizerActive;
  78. /** To know if we have any pending request to adjust view position. */
  79. @property(nonatomic, assign) BOOL hasPendingAdjustRequest;
  80. /*******************************************/
  81. /** Variable to save lastScrollView that was scrolled. */
  82. @property(nullable, nonatomic, weak) UIScrollView *lastScrollView;
  83. /** LastScrollView's initial contentInsets. */
  84. @property(nonatomic, assign) UIEdgeInsets startingContentInsets;
  85. /** LastScrollView's initial scrollIndicatorInsets. */
  86. @property(nonatomic, assign) UIEdgeInsets startingScrollIndicatorInsets;
  87. /** LastScrollView's initial contentOffset. */
  88. @property(nonatomic, assign) CGPoint startingContentOffset;
  89. /*******************************************/
  90. /** To save keyboard animation duration. */
  91. @property(nonatomic, assign) CGFloat animationDuration;
  92. /** To mimic the keyboard animation */
  93. @property(nonatomic, assign) NSInteger animationCurve;
  94. /*******************************************/
  95. /** 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 */
  96. @property(nonnull, nonatomic, strong, readwrite) UITapGestureRecognizer *resignFirstResponderGesture;
  97. /**
  98. moved distance to the top used to maintain distance between keyboard and textField. Most of the time this will be a positive value.
  99. */
  100. @property(nonatomic, assign, readwrite) CGFloat movedDistance;
  101. /*******************************************/
  102. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *disabledDistanceHandlingClasses;
  103. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *enabledDistanceHandlingClasses;
  104. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *disabledToolbarClasses;
  105. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *enabledToolbarClasses;
  106. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *toolbarPreviousNextAllowedClasses;
  107. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *disabledTouchResignedClasses;
  108. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *enabledTouchResignedClasses;
  109. @property(nonatomic, strong, nonnull, readwrite) NSMutableSet<Class> *touchResignedGestureIgnoreClasses;
  110. /*******************************************/
  111. @end
  112. @implementation IQKeyboardManager
  113. {
  114. @package
  115. /*******************************************/
  116. /** To save keyboardWillShowNotification. Needed for enable keyboard functionality. */
  117. NSNotification *_kbShowNotification;
  118. /** To save keyboard size. */
  119. CGRect _kbFrame;
  120. CGSize _keyboardLastNotifySize;
  121. NSMutableDictionary<id<NSCopying>, SizeBlock>* _keyboardSizeObservers;
  122. /*******************************************/
  123. }
  124. //UIKeyboard handling
  125. @synthesize enable = _enable;
  126. @synthesize keyboardDistanceFromTextField = _keyboardDistanceFromTextField;
  127. //Keyboard Appearance handling
  128. @synthesize overrideKeyboardAppearance = _overrideKeyboardAppearance;
  129. @synthesize keyboardAppearance = _keyboardAppearance;
  130. //IQToolbar handling
  131. @synthesize enableAutoToolbar = _enableAutoToolbar;
  132. @synthesize toolbarManageBehaviour = _toolbarManageBehaviour;
  133. @synthesize shouldToolbarUsesTextFieldTintColor = _shouldToolbarUsesTextFieldTintColor;
  134. @synthesize toolbarTintColor = _toolbarTintColor;
  135. @synthesize toolbarBarTintColor = _toolbarBarTintColor;
  136. @synthesize shouldShowToolbarPlaceholder = _shouldShowToolbarPlaceholder;
  137. @synthesize placeholderFont = _placeholderFont;
  138. @synthesize placeholderColor = _placeholderColor;
  139. @synthesize placeholderButtonColor = _placeholderButtonColor;
  140. //Resign handling
  141. @synthesize shouldResignOnTouchOutside = _shouldResignOnTouchOutside;
  142. @synthesize resignFirstResponderGesture = _resignFirstResponderGesture;
  143. //Sound handling
  144. @synthesize shouldPlayInputClicks = _shouldPlayInputClicks;
  145. //Animation handling
  146. @synthesize layoutIfNeededOnUpdate = _layoutIfNeededOnUpdate;
  147. #pragma mark - Initializing functions
  148. /**
  149. Override +load method to enable KeyboardManager when class loader load IQKeyboardManager. Enabling when app starts (No need to write any code)
  150. @Note: If you want to disable `+ (void)load` method, you can add build setting in configurations. Like that:
  151. `{ 'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) IQ_KEYBOARD_MANAGER_LOAD_METHOD_DISABLE=1' }`
  152. */
  153. #if !IQ_KEYBOARD_MANAGER_LOAD_METHOD_DISABLE
  154. +(void)load
  155. {
  156. //Enabling IQKeyboardManager. Loading asynchronous on main thread
  157. [self performSelectorOnMainThread:@selector(sharedManager) withObject:nil waitUntilDone:NO];
  158. }
  159. #endif
  160. /* Singleton Object Initialization. */
  161. -(instancetype)init
  162. {
  163. if (self = [super init])
  164. {
  165. __weak __typeof__(self) weakSelf = self;
  166. static dispatch_once_t onceToken;
  167. dispatch_once(&onceToken, ^{
  168. __strong __typeof__(self) strongSelf = weakSelf;
  169. [strongSelf registerAllNotifications];
  170. //Creating gesture for @shouldResignOnTouchOutside. (Enhancement ID: #14)
  171. strongSelf.resignFirstResponderGesture = [[UITapGestureRecognizer alloc] initWithTarget:strongSelf action:@selector(tapRecognized:)];
  172. strongSelf.resignFirstResponderGesture.cancelsTouchesInView = NO;
  173. [strongSelf.resignFirstResponderGesture setDelegate:strongSelf];
  174. strongSelf.resignFirstResponderGesture.enabled = strongSelf.shouldResignOnTouchOutside;
  175. strongSelf.topViewBeginOrigin = kIQCGPointInvalid;
  176. strongSelf.topViewBeginOriginWhilePopGestureRecognizerActive = kIQCGPointInvalid;
  177. //Setting it's initial values
  178. strongSelf.animationDuration = 0.25;
  179. strongSelf.animationCurve = UIViewAnimationCurveEaseInOut;
  180. [strongSelf setEnable:YES];
  181. [strongSelf setKeyboardDistanceFromTextField:10.0];
  182. [strongSelf setShouldPlayInputClicks:YES];
  183. [strongSelf setShouldResignOnTouchOutside:NO];
  184. [strongSelf setOverrideKeyboardAppearance:NO];
  185. [strongSelf setKeyboardAppearance:UIKeyboardAppearanceDefault];
  186. [strongSelf setEnableAutoToolbar:YES];
  187. [strongSelf setShouldShowToolbarPlaceholder:YES];
  188. [strongSelf setToolbarManageBehaviour:IQAutoToolbarBySubviews];
  189. [strongSelf setLayoutIfNeededOnUpdate:NO];
  190. [strongSelf setShouldToolbarUsesTextFieldTintColor:NO];
  191. //Loading IQToolbar, IQTitleBarButtonItem, IQBarButtonItem to fix first time keyboard appearance delay (Bug ID: #550)
  192. {
  193. //If you experience exception breakpoint issue at below line then try these solutions https://stackoverflow.com/questions/27375640/all-exception-break-point-is-stopping-for-no-reason-on-simulator
  194. UITextField *view = [[UITextField alloc] init];
  195. [view addDoneOnKeyboardWithTarget:nil action:nil];
  196. [view addPreviousNextDoneOnKeyboardWithTarget:nil previousAction:nil nextAction:nil doneAction:nil];
  197. }
  198. strongSelf->_keyboardSizeObservers = [[NSMutableDictionary alloc] init];
  199. //Initializing disabled classes Set.
  200. strongSelf.disabledDistanceHandlingClasses = [[NSMutableSet alloc] initWithObjects:[UITableViewController class],[UIAlertController class], nil];
  201. strongSelf.enabledDistanceHandlingClasses = [[NSMutableSet alloc] init];
  202. strongSelf.disabledToolbarClasses = [[NSMutableSet alloc] initWithObjects:[UIAlertController class], nil];
  203. strongSelf.enabledToolbarClasses = [[NSMutableSet alloc] init];
  204. strongSelf.toolbarPreviousNextAllowedClasses = [[NSMutableSet alloc] initWithObjects:[UITableView class],[UICollectionView class],[IQPreviousNextView class], nil];
  205. strongSelf.disabledTouchResignedClasses = [[NSMutableSet alloc] initWithObjects:[UIAlertController class], nil];
  206. strongSelf.enabledTouchResignedClasses = [[NSMutableSet alloc] init];
  207. strongSelf.touchResignedGestureIgnoreClasses = [[NSMutableSet alloc] initWithObjects:[UIControl class],[UINavigationBar class], nil];
  208. });
  209. }
  210. return self;
  211. }
  212. /* Automatically called from the `+(void)load` method. */
  213. + (IQKeyboardManager*)sharedManager
  214. {
  215. //Singleton instance
  216. static IQKeyboardManager *kbManager;
  217. static dispatch_once_t onceToken;
  218. dispatch_once(&onceToken, ^{
  219. kbManager = [[self alloc] init];
  220. });
  221. return kbManager;
  222. }
  223. #pragma mark - Dealloc
  224. -(void)dealloc
  225. {
  226. // Disable the keyboard manager.
  227. [self setEnable:NO];
  228. //Removing notification observers on dealloc.
  229. [[NSNotificationCenter defaultCenter] removeObserver:self];
  230. }
  231. #pragma mark - Property functions
  232. -(void)setEnable:(BOOL)enable
  233. {
  234. // If not enabled, enable it.
  235. if (enable == YES &&
  236. _enable == NO)
  237. {
  238. //Setting YES to _enable.
  239. _enable = enable;
  240. //If keyboard is currently showing. Sending a fake notification for keyboardWillShow to adjust view according to keyboard.
  241. if (_kbShowNotification) [self keyboardWillShow:_kbShowNotification];
  242. [self showLog:@"Enabled"];
  243. }
  244. //If not disable, desable it.
  245. else if (enable == NO &&
  246. _enable == YES)
  247. {
  248. //Sending a fake notification for keyboardWillHide to retain view's original position.
  249. [self keyboardWillHide:nil];
  250. //Setting NO to _enable.
  251. _enable = enable;
  252. [self showLog:@"Disabled"];
  253. }
  254. //If already disabled.
  255. else if (enable == NO &&
  256. _enable == NO)
  257. {
  258. [self showLog:@"Already Disabled"];
  259. }
  260. //If already enabled.
  261. else if (enable == YES &&
  262. _enable == YES)
  263. {
  264. [self showLog:@"Already Enabled"];
  265. }
  266. }
  267. -(BOOL)privateIsEnabled
  268. {
  269. BOOL enable = _enable;
  270. IQEnableMode enableMode = _textFieldView.enableMode;
  271. if (enableMode == IQEnableModeEnabled)
  272. {
  273. enable = YES;
  274. }
  275. else if (enableMode == IQEnableModeDisabled)
  276. {
  277. enable = NO;
  278. }
  279. else
  280. {
  281. __strong __typeof__(UIView) *strongTextFieldView = _textFieldView;
  282. UIViewController *textFieldViewController = [strongTextFieldView viewContainingController];
  283. if (textFieldViewController)
  284. {
  285. //If it is searchBar textField embedded in Navigation Bar
  286. if ([strongTextFieldView textFieldSearchBar] != nil && [textFieldViewController isKindOfClass:[UINavigationController class]]) {
  287. UINavigationController *navController = (UINavigationController*)textFieldViewController;
  288. if (navController.topViewController) {
  289. textFieldViewController = navController.topViewController;
  290. }
  291. }
  292. if (enable == NO)
  293. {
  294. //If viewController is kind of enable viewController class, then assuming it's enabled.
  295. for (Class enabledClass in _enabledDistanceHandlingClasses)
  296. {
  297. if ([textFieldViewController isKindOfClass:enabledClass])
  298. {
  299. enable = YES;
  300. break;
  301. }
  302. }
  303. }
  304. if (enable)
  305. {
  306. //If viewController is kind of disable viewController class, then assuming it's disable.
  307. for (Class disabledClass in _disabledDistanceHandlingClasses)
  308. {
  309. if ([textFieldViewController isKindOfClass:disabledClass])
  310. {
  311. enable = NO;
  312. break;
  313. }
  314. }
  315. //Special Controllers
  316. if (enable == YES)
  317. {
  318. NSString *classNameString = NSStringFromClass([textFieldViewController class]);
  319. //_UIAlertControllerTextFieldViewController
  320. if ([classNameString containsString:@"UIAlertController"] && [classNameString hasSuffix:@"TextFieldViewController"])
  321. {
  322. enable = NO;
  323. }
  324. }
  325. }
  326. }
  327. }
  328. return enable;
  329. }
  330. // Setting keyboard distance from text field.
  331. -(void)setKeyboardDistanceFromTextField:(CGFloat)keyboardDistanceFromTextField
  332. {
  333. //Can't be less than zero. Minimum is zero.
  334. _keyboardDistanceFromTextField = MAX(keyboardDistanceFromTextField, 0);
  335. [self showLog:[NSString stringWithFormat:@"keyboardDistanceFromTextField: %.2f",_keyboardDistanceFromTextField]];
  336. }
  337. /** Enabling/disable gesture on touching. */
  338. -(void)setShouldResignOnTouchOutside:(BOOL)shouldResignOnTouchOutside
  339. {
  340. [self showLog:[NSString stringWithFormat:@"shouldResignOnTouchOutside: %@",shouldResignOnTouchOutside?@"Yes":@"No"]];
  341. _shouldResignOnTouchOutside = shouldResignOnTouchOutside;
  342. //Enable/Disable gesture recognizer (Enhancement ID: #14)
  343. [_resignFirstResponderGesture setEnabled:[self privateShouldResignOnTouchOutside]];
  344. }
  345. -(BOOL)privateShouldResignOnTouchOutside
  346. {
  347. BOOL shouldResignOnTouchOutside = _shouldResignOnTouchOutside;
  348. __strong __typeof__(UIView) *strongTextFieldView = _textFieldView;
  349. IQEnableMode enableMode = strongTextFieldView.shouldResignOnTouchOutsideMode;
  350. if (enableMode == IQEnableModeEnabled)
  351. {
  352. shouldResignOnTouchOutside = YES;
  353. }
  354. else if (enableMode == IQEnableModeDisabled)
  355. {
  356. shouldResignOnTouchOutside = NO;
  357. }
  358. else
  359. {
  360. UIViewController *textFieldViewController = [strongTextFieldView viewContainingController];
  361. if (textFieldViewController)
  362. {
  363. //If it is searchBar textField embedded in Navigation Bar
  364. if ([strongTextFieldView textFieldSearchBar] != nil && [textFieldViewController isKindOfClass:[UINavigationController class]]) {
  365. UINavigationController *navController = (UINavigationController*)textFieldViewController;
  366. if (navController.topViewController) {
  367. textFieldViewController = navController.topViewController;
  368. }
  369. }
  370. if (shouldResignOnTouchOutside == NO)
  371. {
  372. //If viewController is kind of enable viewController class, then assuming shouldResignOnTouchOutside is enabled.
  373. for (Class enabledClass in _enabledTouchResignedClasses)
  374. {
  375. if ([textFieldViewController isKindOfClass:enabledClass])
  376. {
  377. shouldResignOnTouchOutside = YES;
  378. break;
  379. }
  380. }
  381. }
  382. if (shouldResignOnTouchOutside)
  383. {
  384. //If viewController is kind of disable viewController class, then assuming shouldResignOnTouchOutside is disable.
  385. for (Class disabledClass in _disabledTouchResignedClasses)
  386. {
  387. if ([textFieldViewController isKindOfClass:disabledClass])
  388. {
  389. shouldResignOnTouchOutside = NO;
  390. break;
  391. }
  392. }
  393. //Special Controllers
  394. if (shouldResignOnTouchOutside == YES)
  395. {
  396. NSString *classNameString = NSStringFromClass([textFieldViewController class]);
  397. //_UIAlertControllerTextFieldViewController
  398. if ([classNameString containsString:@"UIAlertController"] && [classNameString hasSuffix:@"TextFieldViewController"])
  399. {
  400. shouldResignOnTouchOutside = NO;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. return shouldResignOnTouchOutside;
  407. }
  408. /** Setter of movedDistance property. */
  409. -(void)setMovedDistance:(CGFloat)movedDistance
  410. {
  411. _movedDistance = movedDistance;
  412. if (self.movedDistanceChanged != nil) {
  413. self.movedDistanceChanged(movedDistance);
  414. }
  415. }
  416. /** Enable/disable autotoolbar. Adding and removing toolbar if required. */
  417. -(void)setEnableAutoToolbar:(BOOL)enableAutoToolbar
  418. {
  419. _enableAutoToolbar = enableAutoToolbar;
  420. [self showLog:[NSString stringWithFormat:@"enableAutoToolbar: %@",enableAutoToolbar?@"Yes":@"No"]];
  421. //If enabled then adding toolbar.
  422. if ([self privateIsEnableAutoToolbar] == YES)
  423. {
  424. [self addToolbarIfRequired];
  425. }
  426. //Else removing toolbar.
  427. else
  428. {
  429. [self removeToolbarIfRequired];
  430. }
  431. }
  432. -(BOOL)privateIsEnableAutoToolbar
  433. {
  434. BOOL enableAutoToolbar = _enableAutoToolbar;
  435. __strong __typeof__(UIView) *strongTextFieldView = _textFieldView;
  436. UIViewController *textFieldViewController = [strongTextFieldView viewContainingController];
  437. if (textFieldViewController)
  438. {
  439. //If it is searchBar textField embedded in Navigation Bar
  440. if ([strongTextFieldView textFieldSearchBar] != nil && [textFieldViewController isKindOfClass:[UINavigationController class]]) {
  441. UINavigationController *navController = (UINavigationController*)textFieldViewController;
  442. if (navController.topViewController) {
  443. textFieldViewController = navController.topViewController;
  444. }
  445. }
  446. if (enableAutoToolbar == NO)
  447. {
  448. //If found any toolbar enabled classes then return.
  449. for (Class enabledToolbarClass in _enabledToolbarClasses)
  450. {
  451. if ([textFieldViewController isKindOfClass:enabledToolbarClass])
  452. {
  453. enableAutoToolbar = YES;
  454. break;
  455. }
  456. }
  457. }
  458. if (enableAutoToolbar)
  459. {
  460. //If found any toolbar disabled classes then return.
  461. for (Class disabledToolbarClass in _disabledToolbarClasses)
  462. {
  463. if ([textFieldViewController isKindOfClass:disabledToolbarClass])
  464. {
  465. enableAutoToolbar = NO;
  466. break;
  467. }
  468. }
  469. //Special Controllers
  470. if (enableAutoToolbar == YES)
  471. {
  472. NSString *classNameString = NSStringFromClass([textFieldViewController class]);
  473. //_UIAlertControllerTextFieldViewController
  474. if ([classNameString containsString:@"UIAlertController"] && [classNameString hasSuffix:@"TextFieldViewController"])
  475. {
  476. enableAutoToolbar = NO;
  477. }
  478. }
  479. }
  480. }
  481. return enableAutoToolbar;
  482. }
  483. #pragma mark - Private Methods
  484. /** Getting keyWindow. */
  485. -(UIWindow *)keyWindow
  486. {
  487. UIView *textFieldView = _textFieldView;
  488. if (textFieldView.window)
  489. {
  490. return textFieldView.window;
  491. }
  492. else
  493. {
  494. static __weak UIWindow *cachedKeyWindow = nil;
  495. /* (Bug ID: #23, #25, #73) */
  496. UIWindow *originalKeyWindow = nil;
  497. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  498. if (@available(iOS 13.0, *)) {
  499. NSSet<UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
  500. for (UIScene *scene in connectedScenes) {
  501. if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
  502. UIWindowScene *windowScene = (UIWindowScene *)scene;
  503. for (UIWindow *window in windowScene.windows) {
  504. if (window.isKeyWindow) {
  505. originalKeyWindow = window;
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. } else
  512. #endif
  513. {
  514. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  515. originalKeyWindow = [UIApplication sharedApplication].keyWindow;
  516. #endif
  517. }
  518. //If original key window is not nil and the cached keywindow is also not original keywindow then changing keywindow.
  519. if (originalKeyWindow)
  520. {
  521. cachedKeyWindow = originalKeyWindow;
  522. }
  523. __strong UIWindow *strongCachedKeyWindow = cachedKeyWindow;
  524. return strongCachedKeyWindow;
  525. }
  526. }
  527. -(void)optimizedAdjustPosition
  528. {
  529. if (_hasPendingAdjustRequest == NO)
  530. {
  531. _hasPendingAdjustRequest = YES;
  532. __weak __typeof__(self) weakSelf = self;
  533. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  534. __strong __typeof__(self) strongSelf = weakSelf;
  535. [strongSelf adjustPosition];
  536. strongSelf.hasPendingAdjustRequest = NO;
  537. }];
  538. }
  539. }
  540. /* Adjusting RootViewController's frame according to interface orientation. */
  541. -(void)adjustPosition
  542. {
  543. UIView *textFieldView = _textFieldView;
  544. // Getting RootViewController. (Bug ID: #1, #4)
  545. UIViewController *rootController = _rootViewController;
  546. // Getting KeyWindow object.
  547. UIWindow *keyWindow = [self keyWindow];
  548. // We are unable to get textField object while keyboard showing on WKWebView's textField. (Bug ID: #11)
  549. if (_hasPendingAdjustRequest == NO ||
  550. textFieldView == nil ||
  551. rootController == nil ||
  552. keyWindow == nil)
  553. return;
  554. CFTimeInterval startTime = CACurrentMediaTime();
  555. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  556. // Converting Rectangle according to window bounds.
  557. CGRect textFieldViewRectInWindow = [[textFieldView superview] convertRect:textFieldView.frame toView:keyWindow];
  558. CGRect textFieldViewRectInRootSuperview = [[textFieldView superview] convertRect:textFieldView.frame toView:rootController.view.superview];
  559. // Getting RootView origin.
  560. CGPoint rootViewOrigin = rootController.view.frame.origin;
  561. //Maintain keyboardDistanceFromTextField
  562. CGFloat specialKeyboardDistanceFromTextField = textFieldView.keyboardDistanceFromTextField;
  563. {
  564. UISearchBar *searchBar = textFieldView.textFieldSearchBar;
  565. if (searchBar)
  566. {
  567. specialKeyboardDistanceFromTextField = searchBar.keyboardDistanceFromTextField;
  568. }
  569. }
  570. CGFloat keyboardDistanceFromTextField = (specialKeyboardDistanceFromTextField == kIQUseDefaultKeyboardDistance)?_keyboardDistanceFromTextField:specialKeyboardDistanceFromTextField;
  571. CGSize kbSize;
  572. {
  573. CGRect kbFrame = _kbFrame;
  574. kbFrame.origin.y -= keyboardDistanceFromTextField;
  575. kbFrame.size.height += keyboardDistanceFromTextField;
  576. //Calculating actual keyboard displayed size, keyboard frame may be different when hardware keyboard is attached (Bug ID: #469) (Bug ID: #381) (Bug ID: #1506)
  577. CGRect intersectRect = CGRectIntersection(kbFrame, keyWindow.frame);
  578. if (CGRectIsNull(intersectRect))
  579. {
  580. kbSize = CGSizeMake(kbFrame.size.width, 0);
  581. }
  582. else
  583. {
  584. kbSize = intersectRect.size;
  585. }
  586. }
  587. CGFloat navigationBarAreaHeight = 0;
  588. if (rootController.navigationController != nil) {
  589. navigationBarAreaHeight = CGRectGetMaxY(rootController.navigationController.navigationBar.frame);
  590. } else {
  591. CGFloat statusBarHeight = 0;
  592. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  593. if (@available(iOS 13.0, *)) {
  594. statusBarHeight = [self keyWindow].windowScene.statusBarManager.statusBarFrame.size.height;
  595. } else
  596. #endif
  597. {
  598. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  599. statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
  600. #endif
  601. }
  602. navigationBarAreaHeight = statusBarHeight;
  603. }
  604. CGFloat layoutAreaHeight = rootController.view.layoutMargins.top;
  605. CGFloat topLayoutGuide = MAX(navigationBarAreaHeight, layoutAreaHeight) + 5;
  606. CGFloat bottomLayoutGuide = ([textFieldView respondsToSelector:@selector(isEditable)] && [textFieldView isKindOfClass:[UIScrollView class]]) ? 0 : rootController.view.layoutMargins.bottom; //Validation of textView for case where there is a tab bar at the bottom or running on iPhone X and textView is at the bottom.
  607. // +Move positive = textField is hidden.
  608. // -Move negative = textField is showing.
  609. // Calculating move position. Common for both normal and special cases.
  610. CGFloat move = MIN(CGRectGetMinY(textFieldViewRectInRootSuperview)-topLayoutGuide, CGRectGetMaxY(textFieldViewRectInWindow)-(CGRectGetHeight(keyWindow.frame)-kbSize.height)+bottomLayoutGuide);
  611. [self showLog:[NSString stringWithFormat:@"Need to move: %.2f",move]];
  612. UIScrollView *superScrollView = nil;
  613. UIScrollView *superView = (UIScrollView*)[textFieldView superviewOfClassType:[UIScrollView class]];
  614. //Getting UIScrollView whose scrolling is enabled. // (Bug ID: #285)
  615. while (superView)
  616. {
  617. if (superView.isScrollEnabled && superView.shouldIgnoreScrollingAdjustment == NO)
  618. {
  619. superScrollView = superView;
  620. break;
  621. }
  622. else
  623. {
  624. // Getting it's superScrollView. // (Enhancement ID: #21, #24)
  625. superView = (UIScrollView*)[superView superviewOfClassType:[UIScrollView class]];
  626. }
  627. }
  628. __strong __typeof__(UIScrollView) *strongLastScrollView = _lastScrollView;
  629. //If there was a lastScrollView. // (Bug ID: #34)
  630. if (strongLastScrollView)
  631. {
  632. //If we can't find current superScrollView, then setting lastScrollView to it's original form.
  633. if (superScrollView == nil)
  634. {
  635. if (UIEdgeInsetsEqualToEdgeInsets(strongLastScrollView.contentInset, _startingContentInsets) == NO)
  636. {
  637. [self showLog:[NSString stringWithFormat:@"Restoring ScrollView contentInset to : %@",NSStringFromUIEdgeInsets(_startingContentInsets)]];
  638. __weak __typeof__(self) weakSelf = self;
  639. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  640. __strong __typeof__(self) strongSelf = weakSelf;
  641. [strongLastScrollView setContentInset:strongSelf.startingContentInsets];
  642. strongLastScrollView.scrollIndicatorInsets = strongSelf.startingScrollIndicatorInsets;
  643. } completion:NULL];
  644. }
  645. if (strongLastScrollView.shouldRestoreScrollViewContentOffset && CGPointEqualToPoint(strongLastScrollView.contentOffset, _startingContentOffset) == NO)
  646. {
  647. [self showLog:[NSString stringWithFormat:@"Restoring ScrollView contentOffset to : %@",NSStringFromCGPoint(_startingContentOffset)]];
  648. BOOL animatedContentOffset = ([textFieldView superviewOfClassType:[UIStackView class] belowView:strongLastScrollView] != nil); // (Bug ID: #1365, #1508, #1541)
  649. if (animatedContentOffset) {
  650. [strongLastScrollView setContentOffset:_startingContentOffset animated:UIView.areAnimationsEnabled];
  651. } else {
  652. strongLastScrollView.contentOffset = _startingContentOffset;
  653. }
  654. }
  655. _startingContentInsets = UIEdgeInsetsZero;
  656. _startingScrollIndicatorInsets = UIEdgeInsetsZero;
  657. _startingContentOffset = CGPointZero;
  658. _lastScrollView = nil;
  659. strongLastScrollView = _lastScrollView;
  660. }
  661. //If both scrollView's are different, then reset lastScrollView to it's original frame and setting current scrollView as last scrollView.
  662. else if (superScrollView != strongLastScrollView)
  663. {
  664. if (UIEdgeInsetsEqualToEdgeInsets(strongLastScrollView.contentInset, _startingContentInsets) == NO)
  665. {
  666. [self showLog:[NSString stringWithFormat:@"Restoring ScrollView contentInset to : %@",NSStringFromUIEdgeInsets(_startingContentInsets)]];
  667. __weak __typeof__(self) weakSelf = self;
  668. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  669. __strong __typeof__(self) strongSelf = weakSelf;
  670. [strongLastScrollView setContentInset:strongSelf.startingContentInsets];
  671. strongLastScrollView.scrollIndicatorInsets = strongSelf.startingScrollIndicatorInsets;
  672. } completion:NULL];
  673. }
  674. if (strongLastScrollView.shouldRestoreScrollViewContentOffset && CGPointEqualToPoint(strongLastScrollView.contentOffset, _startingContentOffset) == NO)
  675. {
  676. [self showLog:[NSString stringWithFormat:@"Restoring ScrollView contentOffset to : %@",NSStringFromCGPoint(_startingContentOffset)]];
  677. BOOL animatedContentOffset = ([textFieldView superviewOfClassType:[UIStackView class] belowView:strongLastScrollView] != nil); // (Bug ID: #1365, #1508, #1541)
  678. if (animatedContentOffset) {
  679. [strongLastScrollView setContentOffset:_startingContentOffset animated:UIView.areAnimationsEnabled];
  680. } else {
  681. strongLastScrollView.contentOffset = _startingContentOffset;
  682. }
  683. }
  684. _lastScrollView = superScrollView;
  685. strongLastScrollView = _lastScrollView;
  686. _startingContentInsets = superScrollView.contentInset;
  687. _startingContentOffset = superScrollView.contentOffset;
  688. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  689. if (@available(iOS 11.1, *)) {
  690. _startingScrollIndicatorInsets = superScrollView.verticalScrollIndicatorInsets;
  691. } else
  692. #endif
  693. {
  694. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  695. _startingScrollIndicatorInsets = superScrollView.scrollIndicatorInsets;
  696. #endif
  697. }
  698. [self showLog:[NSString stringWithFormat:@"Saving New contentInset: %@ and contentOffset : %@",NSStringFromUIEdgeInsets(_startingContentInsets),NSStringFromCGPoint(_startingContentOffset)]];
  699. }
  700. //Else the case where superScrollView == lastScrollView means we are on same scrollView after switching to different textField. So doing nothing
  701. }
  702. //If there was no lastScrollView and we found a current scrollView. then setting it as lastScrollView.
  703. else if(superScrollView)
  704. {
  705. _lastScrollView = superScrollView;
  706. strongLastScrollView = _lastScrollView;
  707. _startingContentInsets = superScrollView.contentInset;
  708. _startingContentOffset = superScrollView.contentOffset;
  709. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  710. if (@available(iOS 11.1, *)) {
  711. _startingScrollIndicatorInsets = superScrollView.verticalScrollIndicatorInsets;
  712. } else
  713. #endif
  714. {
  715. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  716. _startingScrollIndicatorInsets = superScrollView.scrollIndicatorInsets;
  717. #endif
  718. }
  719. [self showLog:[NSString stringWithFormat:@"Saving contentInset: %@ and contentOffset : %@",NSStringFromUIEdgeInsets(_startingContentInsets),NSStringFromCGPoint(_startingContentOffset)]];
  720. }
  721. // Special case for ScrollView.
  722. {
  723. // If we found lastScrollView then setting it's contentOffset to show textField.
  724. if (strongLastScrollView)
  725. {
  726. //Saving
  727. UIView *lastView = textFieldView;
  728. superScrollView = strongLastScrollView;
  729. //Looping in upper hierarchy until we don't found any scrollView in it's upper hirarchy till UIWindow object.
  730. while (superScrollView)
  731. {
  732. BOOL shouldContinue = NO;
  733. if (move > 0)
  734. {
  735. shouldContinue = move > (-superScrollView.contentOffset.y-superScrollView.contentInset.top);
  736. }
  737. else
  738. {
  739. //Special treatment for UITableView due to their cell reusing logic
  740. if ([superScrollView isKindOfClass:[UITableView class]])
  741. {
  742. shouldContinue = superScrollView.contentOffset.y>0;
  743. UITableView *tableView = (UITableView*)superScrollView;
  744. UITableViewCell *tableCell = nil;
  745. NSIndexPath *indexPath = nil;
  746. NSIndexPath *previousIndexPath = nil;
  747. if (shouldContinue &&
  748. (tableCell = (UITableViewCell*)[textFieldView superviewOfClassType:[UITableViewCell class]]) &&
  749. (indexPath = [tableView indexPathForCell:tableCell]) &&
  750. (previousIndexPath = [tableView previousIndexPathOfIndexPath:indexPath]))
  751. {
  752. CGRect previousCellRect = [tableView rectForRowAtIndexPath:previousIndexPath];
  753. if (CGRectIsEmpty(previousCellRect) == NO)
  754. {
  755. CGRect previousCellRectInRootSuperview = [tableView convertRect:previousCellRect toView:rootController.view.superview];
  756. move = MIN(0, CGRectGetMaxY(previousCellRectInRootSuperview) - topLayoutGuide);
  757. }
  758. }
  759. }
  760. //Special treatment for UICollectionView due to their cell reusing logic
  761. else if ([superScrollView isKindOfClass:[UICollectionView class]])
  762. {
  763. shouldContinue = superScrollView.contentOffset.y>0;
  764. UICollectionView *collectionView = (UICollectionView*)superScrollView;
  765. UICollectionViewCell *collectionCell = nil;
  766. NSIndexPath *indexPath = nil;
  767. NSIndexPath *previousIndexPath = nil;
  768. if (shouldContinue &&
  769. (collectionCell = (UICollectionViewCell*)[textFieldView superviewOfClassType:[UICollectionViewCell class]]) &&
  770. (indexPath = [collectionView indexPathForCell:collectionCell]) &&
  771. (previousIndexPath = [collectionView previousIndexPathOfIndexPath:indexPath]))
  772. {
  773. UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:previousIndexPath];
  774. CGRect previousCellRect = attributes.frame;
  775. if (CGRectIsEmpty(previousCellRect) == NO)
  776. {
  777. CGRect previousCellRectInRootSuperview = [collectionView convertRect:previousCellRect toView:rootController.view.superview];
  778. move = MIN(0, CGRectGetMaxY(previousCellRectInRootSuperview) - topLayoutGuide);
  779. }
  780. }
  781. }
  782. else
  783. {
  784. //If the textField is hidden at the top
  785. shouldContinue = textFieldViewRectInRootSuperview.origin.y < topLayoutGuide;
  786. if (shouldContinue) {
  787. move = MIN(0, textFieldViewRectInRootSuperview.origin.y - topLayoutGuide);
  788. }
  789. }
  790. }
  791. if (shouldContinue == NO)
  792. {
  793. move = 0;
  794. break;
  795. }
  796. UIScrollView *nextScrollView = nil;
  797. UIScrollView *tempScrollView = (UIScrollView*)[superScrollView superviewOfClassType:[UIScrollView class]];
  798. //Getting UIScrollView whose scrolling is enabled. // (Bug ID: #285)
  799. while (tempScrollView)
  800. {
  801. if (tempScrollView.isScrollEnabled && tempScrollView.shouldIgnoreScrollingAdjustment == NO)
  802. {
  803. nextScrollView = tempScrollView;
  804. break;
  805. }
  806. else
  807. {
  808. // Getting it's superScrollView. // (Enhancement ID: #21, #24)
  809. tempScrollView = (UIScrollView*)[tempScrollView superviewOfClassType:[UIScrollView class]];
  810. }
  811. }
  812. //Getting lastViewRect.
  813. CGRect lastViewRect = [[lastView superview] convertRect:lastView.frame toView:superScrollView];
  814. //Calculating the expected Y offset from move and scrollView's contentOffset.
  815. CGFloat shouldOffsetY = superScrollView.contentOffset.y - MIN(superScrollView.contentOffset.y,-move);
  816. //Rearranging the expected Y offset according to the view.
  817. shouldOffsetY = MIN(shouldOffsetY, lastViewRect.origin.y);
  818. //[textFieldView isKindOfClass:[UITextView class]] If is a UITextView type
  819. //[superScrollView superviewOfClassType:[UIScrollView class]] == nil If processing scrollView is last scrollView in upper hierarchy (there is no other scrollView upper hierarchy.)
  820. //shouldOffsetY >= 0 shouldOffsetY must be greater than in order to keep distance from navigationBar (Bug ID: #92)
  821. if ([textFieldView respondsToSelector:@selector(isEditable)] && [textFieldView isKindOfClass:[UIScrollView class]] &&
  822. nextScrollView == nil &&
  823. (shouldOffsetY >= 0))
  824. {
  825. // Converting Rectangle according to window bounds.
  826. CGRect currentTextFieldViewRect = [[textFieldView superview] convertRect:textFieldView.frame toView:keyWindow];
  827. //Calculating expected fix distance which needs to be managed from navigation bar
  828. CGFloat expectedFixDistance = CGRectGetMinY(currentTextFieldViewRect) - topLayoutGuide;
  829. //Now if expectedOffsetY (superScrollView.contentOffset.y + expectedFixDistance) is lower than current shouldOffsetY, which means we're in a position where navigationBar up and hide, then reducing shouldOffsetY with expectedOffsetY (superScrollView.contentOffset.y + expectedFixDistance)
  830. shouldOffsetY = MIN(shouldOffsetY, superScrollView.contentOffset.y + expectedFixDistance);
  831. //Setting move to 0 because now we don't want to move any view anymore (All will be managed by our contentInset logic.
  832. move = 0;
  833. }
  834. else
  835. {
  836. //Subtracting the Y offset from the move variable, because we are going to change scrollView's contentOffset.y to shouldOffsetY.
  837. move -= (shouldOffsetY-superScrollView.contentOffset.y);
  838. }
  839. CGPoint newContentOffset = CGPointMake(superScrollView.contentOffset.x, shouldOffsetY);
  840. if (CGPointEqualToPoint(superScrollView.contentOffset, newContentOffset) == NO)
  841. {
  842. __weak __typeof__(self) weakSelf = self;
  843. //Getting problem while using `setContentOffset:animated:`, So I used animation API.
  844. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  845. __strong __typeof__(self) strongSelf = weakSelf;
  846. [strongSelf showLog:[NSString stringWithFormat:@"Adjusting %.2f to %@ ContentOffset",(superScrollView.contentOffset.y-shouldOffsetY),[superScrollView _IQDescription]]];
  847. [strongSelf showLog:[NSString stringWithFormat:@"Remaining Move: %.2f",move]];
  848. BOOL animatedContentOffset = ([textFieldView superviewOfClassType:[UIStackView class] belowView:superScrollView] != nil); // (Bug ID: #1365, #1508, #1541)
  849. if (animatedContentOffset) {
  850. [superScrollView setContentOffset:newContentOffset animated:UIView.areAnimationsEnabled];
  851. } else {
  852. superScrollView.contentOffset = newContentOffset;
  853. }
  854. } completion:^(BOOL finished){
  855. __strong __typeof__(self) strongSelf = weakSelf;
  856. if ([superScrollView isKindOfClass:[UITableView class]] || [superScrollView isKindOfClass:[UICollectionView class]])
  857. {
  858. //This will update the next/previous states
  859. [strongSelf addToolbarIfRequired];
  860. }
  861. }];
  862. }
  863. // Getting next lastView & superScrollView.
  864. lastView = superScrollView;
  865. superScrollView = nextScrollView;
  866. }
  867. //Updating contentInset
  868. if (strongLastScrollView.shouldIgnoreContentInsetAdjustment == NO)
  869. {
  870. CGRect lastScrollViewRect = [[strongLastScrollView superview] convertRect:strongLastScrollView.frame toView:keyWindow];
  871. CGFloat bottomInset = (kbSize.height)-(CGRectGetHeight(keyWindow.frame)-CGRectGetMaxY(lastScrollViewRect));
  872. CGFloat bottomScrollIndicatorInset = bottomInset - keyboardDistanceFromTextField;
  873. // Update the insets so that the scroll vew doesn't shift incorrectly when the offset is near the bottom of the scroll view.
  874. bottomInset = MAX(_startingContentInsets.bottom, bottomInset);
  875. bottomScrollIndicatorInset = MAX(_startingScrollIndicatorInsets.bottom, bottomScrollIndicatorInset);
  876. if (@available(iOS 11, *)) {
  877. bottomInset -= strongLastScrollView.safeAreaInsets.bottom;
  878. bottomScrollIndicatorInset -= strongLastScrollView.safeAreaInsets.bottom;
  879. }
  880. UIEdgeInsets movedInsets = strongLastScrollView.contentInset;
  881. movedInsets.bottom = bottomInset;
  882. if (UIEdgeInsetsEqualToEdgeInsets(strongLastScrollView.contentInset, movedInsets) == NO)
  883. {
  884. [self showLog:[NSString stringWithFormat:@"old ContentInset : %@ new ContentInset : %@", NSStringFromUIEdgeInsets(strongLastScrollView.contentInset), NSStringFromUIEdgeInsets(movedInsets)]];
  885. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  886. strongLastScrollView.contentInset = movedInsets;
  887. UIEdgeInsets newScrollIndicatorInset;
  888. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  889. if (@available(iOS 11.1, *)) {
  890. newScrollIndicatorInset = strongLastScrollView.verticalScrollIndicatorInsets;
  891. } else
  892. #endif
  893. {
  894. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  895. newScrollIndicatorInset = strongLastScrollView.scrollIndicatorInsets;
  896. #endif
  897. }
  898. newScrollIndicatorInset.bottom = bottomScrollIndicatorInset;
  899. strongLastScrollView.scrollIndicatorInsets = newScrollIndicatorInset;
  900. } completion:NULL];
  901. }
  902. }
  903. }
  904. //Going ahead. No else if.
  905. }
  906. {
  907. //Special case for UITextView(Readjusting textView.contentInset when textView hight is too big to fit on screen)
  908. //_lastScrollView If not having inside any scrollView, (now contentInset manages the full screen textView.
  909. //[textFieldView isKindOfClass:[UITextView class]] If is a UITextView type
  910. if ([textFieldView isKindOfClass:[UIScrollView class]] && [(UIScrollView*)textFieldView isScrollEnabled] && [textFieldView respondsToSelector:@selector(isEditable)])
  911. {
  912. UIScrollView *textView = (UIScrollView*)textFieldView;
  913. CGFloat keyboardYPosition = CGRectGetHeight(keyWindow.frame)-(kbSize.height-keyboardDistanceFromTextField);
  914. CGRect rootSuperViewFrameInWindow = [rootController.view.superview convertRect:rootController.view.superview.bounds toView:keyWindow];
  915. CGFloat keyboardOverlapping = CGRectGetMaxY(rootSuperViewFrameInWindow) - keyboardYPosition;
  916. CGFloat textViewHeight = MIN(CGRectGetHeight(textFieldView.frame), (CGRectGetHeight(rootSuperViewFrameInWindow)-topLayoutGuide-keyboardOverlapping));
  917. if (textFieldView.frame.size.height-textView.contentInset.bottom>textViewHeight)
  918. {
  919. //_isTextViewContentInsetChanged, If frame is not change by library in past, then saving user textView properties (Bug ID: #92)
  920. if (self.isTextViewContentInsetChanged == NO)
  921. {
  922. self.startingTextViewContentInsets = textView.contentInset;
  923. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  924. if (@available(iOS 11.1, *)) {
  925. self.startingTextViewScrollIndicatorInsets = textView.verticalScrollIndicatorInsets;
  926. } else
  927. #endif
  928. {
  929. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  930. self.startingTextViewScrollIndicatorInsets = textView.scrollIndicatorInsets;
  931. #endif
  932. }
  933. }
  934. CGFloat bottomInset = textFieldView.frame.size.height-textViewHeight;
  935. if (@available(iOS 11, *)) {
  936. bottomInset -= textFieldView.safeAreaInsets.bottom;
  937. }
  938. UIEdgeInsets newContentInset = textView.contentInset;
  939. newContentInset.bottom = bottomInset;
  940. self.isTextViewContentInsetChanged = YES;
  941. if (UIEdgeInsetsEqualToEdgeInsets(textView.contentInset, newContentInset) == NO)
  942. {
  943. __weak __typeof__(self) weakSelf = self;
  944. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  945. __strong __typeof__(self) strongSelf = weakSelf;
  946. [strongSelf showLog:[NSString stringWithFormat:@"Old UITextView.contentInset : %@ New UITextView.contentInset : %@", NSStringFromUIEdgeInsets(textView.contentInset), NSStringFromUIEdgeInsets(textView.contentInset)]];
  947. textView.contentInset = newContentInset;
  948. textView.scrollIndicatorInsets = newContentInset;
  949. } completion:NULL];
  950. }
  951. }
  952. }
  953. {
  954. __weak __typeof__(self) weakSelf = self;
  955. // +Positive or zero.
  956. if (move>=0)
  957. {
  958. rootViewOrigin.y -= move;
  959. // From now prevent keyboard manager to slide up the rootView to more than keyboard height. (Bug ID: #93)
  960. rootViewOrigin.y = MAX(rootViewOrigin.y, MIN(0, -(kbSize.height-keyboardDistanceFromTextField)));
  961. [self showLog:@"Moving Upward"];
  962. // Setting adjusted rootViewOrigin.ty
  963. //Used UIViewAnimationOptionBeginFromCurrentState to minimize strange animations.
  964. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  965. __strong __typeof__(self) strongSelf = weakSelf;
  966. // Setting it's new frame
  967. CGRect rect = rootController.view.frame;
  968. rect.origin = rootViewOrigin;
  969. rootController.view.frame = rect;
  970. //Animating content if needed (Bug ID: #204)
  971. if (strongSelf.layoutIfNeededOnUpdate)
  972. {
  973. //Animating content (Bug ID: #160)
  974. [rootController.view setNeedsLayout];
  975. [rootController.view layoutIfNeeded];
  976. }
  977. [strongSelf showLog:[NSString stringWithFormat:@"Set %@ origin to : %@",rootController,NSStringFromCGPoint(rootViewOrigin)]];
  978. } completion:NULL];
  979. self.movedDistance = (_topViewBeginOrigin.y-rootViewOrigin.y);
  980. }
  981. // -Negative
  982. else
  983. {
  984. CGFloat disturbDistance = rootController.view.frame.origin.y-_topViewBeginOrigin.y;
  985. // disturbDistance Negative = frame disturbed. Pull Request #3
  986. // disturbDistance positive = frame not disturbed.
  987. if(disturbDistance<=0)
  988. {
  989. rootViewOrigin.y -= MAX(move, disturbDistance);
  990. [self showLog:@"Moving Downward"];
  991. // Setting adjusted rootViewRect
  992. //Used UIViewAnimationOptionBeginFromCurrentState to minimize strange animations.
  993. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  994. __strong __typeof__(self) strongSelf = weakSelf;
  995. // Setting it's new frame
  996. CGRect rect = rootController.view.frame;
  997. rect.origin = rootViewOrigin;
  998. rootController.view.frame = rect;
  999. //Animating content if needed (Bug ID: #204)
  1000. if (strongSelf.layoutIfNeededOnUpdate)
  1001. {
  1002. //Animating content (Bug ID: #160)
  1003. [rootController.view setNeedsLayout];
  1004. [rootController.view layoutIfNeeded];
  1005. }
  1006. [strongSelf showLog:[NSString stringWithFormat:@"Set %@ origin to : %@",rootController,NSStringFromCGPoint(rootViewOrigin)]];
  1007. } completion:NULL];
  1008. self.movedDistance = (_topViewBeginOrigin.y-rootController.view.frame.origin.y);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1014. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1015. }
  1016. -(void)restorePosition
  1017. {
  1018. _hasPendingAdjustRequest = NO;
  1019. // Setting rootViewController frame to it's original position. // (Bug ID: #18)
  1020. if (_rootViewController && CGPointEqualToPoint(_topViewBeginOrigin, kIQCGPointInvalid) == false)
  1021. {
  1022. __weak __typeof__(self) weakSelf = self;
  1023. //Used UIViewAnimationOptionBeginFromCurrentState to minimize strange animations.
  1024. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  1025. __strong __typeof__(self) strongSelf = weakSelf;
  1026. UIViewController *strongRootController = strongSelf.rootViewController;
  1027. {
  1028. [strongSelf showLog:[NSString stringWithFormat:@"Restoring %@ origin to : %@",strongRootController,NSStringFromCGPoint(strongSelf.topViewBeginOrigin)]];
  1029. //Restoring
  1030. CGRect rect = strongRootController.view.frame;
  1031. rect.origin = strongSelf.topViewBeginOrigin;
  1032. strongRootController.view.frame = rect;
  1033. strongSelf.movedDistance = 0;
  1034. if (strongRootController.navigationController.interactivePopGestureRecognizer.state == UIGestureRecognizerStateBegan) {
  1035. strongSelf.rootViewControllerWhilePopGestureRecognizerActive = strongRootController;
  1036. strongSelf.topViewBeginOriginWhilePopGestureRecognizerActive = strongSelf.topViewBeginOrigin;
  1037. }
  1038. //Animating content if needed (Bug ID: #204)
  1039. if (strongSelf.layoutIfNeededOnUpdate)
  1040. {
  1041. //Animating content (Bug ID: #160)
  1042. [strongRootController.view setNeedsLayout];
  1043. [strongRootController.view layoutIfNeeded];
  1044. }
  1045. }
  1046. } completion:NULL];
  1047. _rootViewController = nil;
  1048. }
  1049. }
  1050. #pragma mark - Public Methods
  1051. /* Refreshes textField/textView position if any external changes is explicitly made by user. */
  1052. - (void)reloadLayoutIfNeeded
  1053. {
  1054. if ([self privateIsEnabled] == YES)
  1055. {
  1056. UIView *textFieldView = _textFieldView;
  1057. if (textFieldView &&
  1058. _keyboardShowing == YES &&
  1059. CGPointEqualToPoint(_topViewBeginOrigin, kIQCGPointInvalid) == false &&
  1060. [textFieldView isAlertViewTextField] == NO)
  1061. {
  1062. [self optimizedAdjustPosition];
  1063. }
  1064. }
  1065. }
  1066. #pragma mark - UIKeyboad Notification methods
  1067. /* UIKeyboardWillShowNotification. */
  1068. -(void)keyboardWillShow:(NSNotification*)aNotification
  1069. {
  1070. _kbShowNotification = aNotification;
  1071. // Boolean to know keyboard is showing/hiding
  1072. _keyboardShowing = YES;
  1073. // Getting keyboard animation.
  1074. NSInteger curve = [[aNotification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue];
  1075. _animationCurve = curve<<16;
  1076. // Getting keyboard animation duration
  1077. CGFloat duration = [[aNotification userInfo][UIKeyboardAnimationDurationUserInfoKey] floatValue];
  1078. //Saving animation duration
  1079. if (duration != 0.0) _animationDuration = duration;
  1080. CGRect oldKBFrame = _kbFrame;
  1081. // Getting UIKeyboardSize.
  1082. _kbFrame = [[aNotification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
  1083. [self notifyKeyboardSize:_kbFrame.size];
  1084. if ([self privateIsEnabled] == NO)
  1085. {
  1086. [self restorePosition];
  1087. _topViewBeginOrigin = kIQCGPointInvalid;
  1088. return;
  1089. }
  1090. CFTimeInterval startTime = CACurrentMediaTime();
  1091. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1092. UIView *textFieldView = _textFieldView;
  1093. if (textFieldView && CGPointEqualToPoint(_topViewBeginOrigin, kIQCGPointInvalid)) // (Bug ID: #5)
  1094. {
  1095. // keyboard is not showing(At the beginning only). We should save rootViewRect.
  1096. UIViewController *rootController = [textFieldView parentContainerViewController];
  1097. _rootViewController = rootController;
  1098. if (_rootViewControllerWhilePopGestureRecognizerActive == rootController)
  1099. {
  1100. _topViewBeginOrigin = _topViewBeginOriginWhilePopGestureRecognizerActive;
  1101. }
  1102. else
  1103. {
  1104. _topViewBeginOrigin = rootController.view.frame.origin;
  1105. }
  1106. _rootViewControllerWhilePopGestureRecognizerActive = nil;
  1107. _topViewBeginOriginWhilePopGestureRecognizerActive = kIQCGPointInvalid;
  1108. [self showLog:[NSString stringWithFormat:@"Saving %@ beginning origin: %@",rootController,NSStringFromCGPoint(_topViewBeginOrigin)]];
  1109. }
  1110. //If last restored keyboard size is different(any orientation accure), then refresh. otherwise not.
  1111. if (!CGRectEqualToRect(_kbFrame, oldKBFrame))
  1112. {
  1113. //If _textFieldView is inside AlertView then do nothing. (Bug ID: #37, #74, #76)
  1114. //See notes:- https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html If it is AlertView textField then do not affect anything (Bug ID: #70).
  1115. if (_keyboardShowing == YES &&
  1116. textFieldView &&
  1117. [textFieldView isAlertViewTextField] == NO)
  1118. {
  1119. [self optimizedAdjustPosition];
  1120. }
  1121. }
  1122. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1123. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1124. }
  1125. /* UIKeyboardDidShowNotification. */
  1126. - (void)keyboardDidShow:(NSNotification*)aNotification
  1127. {
  1128. if ([self privateIsEnabled] == NO) return;
  1129. CFTimeInterval startTime = CACurrentMediaTime();
  1130. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1131. [self showLog:[NSString stringWithFormat:@"Notification Object: %@", aNotification.object]];
  1132. UIView *textFieldView = _textFieldView;
  1133. // Getting topMost ViewController.
  1134. UIViewController *controller = [textFieldView topMostController];
  1135. //If _textFieldView viewController is presented as formSheet, then adjustPosition again because iOS internally update formSheet frame on keyboardShown. (Bug ID: #37, #74, #76)
  1136. if (_keyboardShowing == YES &&
  1137. textFieldView &&
  1138. (controller.modalPresentationStyle == UIModalPresentationFormSheet || controller.modalPresentationStyle == UIModalPresentationPageSheet) &&
  1139. [textFieldView isAlertViewTextField] == NO)
  1140. {
  1141. [self optimizedAdjustPosition];
  1142. }
  1143. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1144. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1145. }
  1146. /* UIKeyboardWillHideNotification. So setting rootViewController to it's default frame. */
  1147. - (void)keyboardWillHide:(NSNotification*)aNotification
  1148. {
  1149. //If it's not a fake notification generated by [self setEnable:NO].
  1150. if (aNotification) _kbShowNotification = nil;
  1151. // Boolean to know keyboard is showing/hiding
  1152. _keyboardShowing = NO;
  1153. // Getting keyboard animation duration
  1154. CGFloat aDuration = [[aNotification userInfo][UIKeyboardAnimationDurationUserInfoKey] floatValue];
  1155. if (aDuration!= 0.0f)
  1156. {
  1157. _animationDuration = aDuration;
  1158. }
  1159. //If not enabled then do nothing.
  1160. if ([self privateIsEnabled] == NO) return;
  1161. CFTimeInterval startTime = CACurrentMediaTime();
  1162. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1163. [self showLog:[NSString stringWithFormat:@"Notification Object: %@", aNotification.object]];
  1164. //Commented due to #56. Added all the conditions below to handle WKWebView's textFields. (Bug ID: #56)
  1165. // We are unable to get textField object while keyboard showing on WKWebView's textField. (Bug ID: #11)
  1166. // if (_textFieldView == nil) return;
  1167. //Restoring the contentOffset of the lastScrollView
  1168. __strong __typeof__(UIScrollView) *strongLastScrollView = _lastScrollView;
  1169. if (strongLastScrollView)
  1170. {
  1171. __weak __typeof__(self) weakSelf = self;
  1172. __weak __typeof__(UIView) *weakTextFieldView = self.textFieldView;
  1173. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  1174. __strong __typeof__(self) strongSelf = weakSelf;
  1175. __strong __typeof__(UIView) *strongTextFieldView = weakTextFieldView;
  1176. if (UIEdgeInsetsEqualToEdgeInsets(strongLastScrollView.contentInset, strongSelf.startingContentInsets) == NO)
  1177. {
  1178. [strongSelf showLog:[NSString stringWithFormat:@"Restoring ScrollView contentInset to : %@",NSStringFromUIEdgeInsets(strongSelf.startingContentInsets)]];
  1179. strongLastScrollView.contentInset = strongSelf.startingContentInsets;
  1180. strongLastScrollView.scrollIndicatorInsets = strongSelf.startingScrollIndicatorInsets;
  1181. }
  1182. if (strongLastScrollView.shouldRestoreScrollViewContentOffset && CGPointEqualToPoint(strongLastScrollView.contentOffset, strongSelf.startingContentOffset) == NO)
  1183. {
  1184. [strongSelf showLog:[NSString stringWithFormat:@"Restoring ScrollView contentOffset to : %@",NSStringFromCGPoint(strongSelf.startingContentOffset)]];
  1185. BOOL animatedContentOffset = ([strongTextFieldView superviewOfClassType:[UIStackView class] belowView:strongLastScrollView] != nil); // (Bug ID: #1365, #1508, #1541)
  1186. if (animatedContentOffset) {
  1187. [strongLastScrollView setContentOffset:strongSelf.startingContentOffset animated:UIView.areAnimationsEnabled];
  1188. } else {
  1189. strongLastScrollView.contentOffset = strongSelf.startingContentOffset;
  1190. }
  1191. }
  1192. // TODO: restore scrollView state
  1193. // This is temporary solution. Have to implement the save and restore scrollView state
  1194. UIScrollView *superscrollView = strongLastScrollView;
  1195. do
  1196. {
  1197. CGSize contentSize = CGSizeMake(MAX(superscrollView.contentSize.width, CGRectGetWidth(superscrollView.frame)), MAX(superscrollView.contentSize.height, CGRectGetHeight(superscrollView.frame)));
  1198. CGFloat minimumY = contentSize.height-CGRectGetHeight(superscrollView.frame);
  1199. if (minimumY<superscrollView.contentOffset.y)
  1200. {
  1201. CGPoint newContentOffset = CGPointMake(superscrollView.contentOffset.x, minimumY);
  1202. if (CGPointEqualToPoint(superscrollView.contentOffset, newContentOffset) == NO)
  1203. {
  1204. [self showLog:[NSString stringWithFormat:@"Restoring contentOffset to : %@",NSStringFromCGPoint(newContentOffset)]];
  1205. BOOL animatedContentOffset = ([strongSelf.textFieldView superviewOfClassType:[UIStackView class] belowView:superscrollView] != nil); // (Bug ID: #1365, #1508, #1541)
  1206. if (animatedContentOffset) {
  1207. [superscrollView setContentOffset:newContentOffset animated:UIView.areAnimationsEnabled];
  1208. } else {
  1209. superscrollView.contentOffset = newContentOffset;
  1210. }
  1211. }
  1212. }
  1213. } while ((superscrollView = (UIScrollView*)[superscrollView superviewOfClassType:[UIScrollView class]]));
  1214. } completion:NULL];
  1215. }
  1216. [self restorePosition];
  1217. //Reset all values
  1218. _lastScrollView = nil;
  1219. _kbFrame = CGRectZero;
  1220. [self notifyKeyboardSize:_kbFrame.size];
  1221. _startingContentInsets = UIEdgeInsetsZero;
  1222. _startingScrollIndicatorInsets = UIEdgeInsetsZero;
  1223. _startingContentOffset = CGPointZero;
  1224. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1225. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1226. }
  1227. /* UIKeyboardDidHideNotification. So topViewBeginRect can be set to CGRectZero. */
  1228. - (void)keyboardDidHide:(NSNotification*)aNotification
  1229. {
  1230. CFTimeInterval startTime = CACurrentMediaTime();
  1231. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1232. [self showLog:[NSString stringWithFormat:@"Notification Object: %@", aNotification.object]];
  1233. _topViewBeginOrigin = kIQCGPointInvalid;
  1234. _kbFrame = CGRectZero;
  1235. [self notifyKeyboardSize:_kbFrame.size];
  1236. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1237. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1238. }
  1239. -(void)registerKeyboardSizeChangeWithIdentifier:(nonnull id<NSCopying>)identifier sizeHandler:(void (^_Nonnull)(CGSize size))sizeHandler
  1240. {
  1241. _keyboardSizeObservers[identifier] = sizeHandler;
  1242. }
  1243. -(void)unregisterKeyboardSizeChangeWithIdentifier:(nonnull id<NSCopying>)identifier
  1244. {
  1245. _keyboardSizeObservers[identifier] = nil;
  1246. }
  1247. -(void)notifyKeyboardSize:(CGSize)size
  1248. {
  1249. if (!CGSizeEqualToSize(size, _keyboardLastNotifySize)) {
  1250. _keyboardLastNotifySize = size;
  1251. for (SizeBlock block in _keyboardSizeObservers.allValues) {
  1252. block(size);
  1253. }
  1254. }
  1255. }
  1256. #pragma mark - UITextFieldView Delegate methods
  1257. /** UITextFieldTextDidBeginEditingNotification, UITextViewTextDidBeginEditingNotification. Fetching UITextFieldView object. */
  1258. -(void)textFieldViewDidBeginEditing:(NSNotification*)notification
  1259. {
  1260. UIView *object = (UIView*)notification.object;
  1261. if (object.window.isKeyWindow == NO) {
  1262. return;
  1263. }
  1264. CFTimeInterval startTime = CACurrentMediaTime();
  1265. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1266. [self showLog:[NSString stringWithFormat:@"Notification Object: %@", notification.object]];
  1267. // Getting object
  1268. _textFieldView = object;
  1269. UIView *textFieldView = _textFieldView;
  1270. if (_overrideKeyboardAppearance == YES)
  1271. {
  1272. UITextField *textField = (UITextField*)textFieldView;
  1273. if ([textField respondsToSelector:@selector(keyboardAppearance)])
  1274. {
  1275. //If keyboard appearance is not like the provided appearance
  1276. if (textField.keyboardAppearance != _keyboardAppearance)
  1277. {
  1278. //Setting textField keyboard appearance and reloading inputViews.
  1279. textField.keyboardAppearance = _keyboardAppearance;
  1280. [textField reloadInputViews];
  1281. }
  1282. }
  1283. }
  1284. //If autoToolbar enable, then add toolbar on all the UITextField/UITextView's if required.
  1285. if ([self privateIsEnableAutoToolbar])
  1286. {
  1287. //UITextView special case. Keyboard Notification is firing before textView notification so we need to reload it's inputViews.
  1288. if ([textFieldView respondsToSelector:@selector(isEditable)] && [textFieldView isKindOfClass:[UIScrollView class]] &&
  1289. textFieldView.inputAccessoryView == nil)
  1290. {
  1291. __weak __typeof__(self) weakSelf = self;
  1292. [UIView animateWithDuration:0.00001 delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  1293. __strong __typeof__(self) strongSelf = weakSelf;
  1294. [strongSelf addToolbarIfRequired];
  1295. } completion:^(BOOL finished) {
  1296. __strong __typeof__(self) strongSelf = weakSelf;
  1297. //On textView toolbar didn't appear on first time, so forcing textView to reload it's inputViews.
  1298. [strongSelf.textFieldView reloadInputViews];
  1299. }];
  1300. }
  1301. //Else adding toolbar
  1302. else
  1303. {
  1304. [self addToolbarIfRequired];
  1305. }
  1306. }
  1307. else
  1308. {
  1309. [self removeToolbarIfRequired];
  1310. }
  1311. //Adding Geture recognizer to window (Enhancement ID: #14)
  1312. [_resignFirstResponderGesture setEnabled:[self privateShouldResignOnTouchOutside]];
  1313. [textFieldView.window addGestureRecognizer:_resignFirstResponderGesture];
  1314. if ([self privateIsEnabled] == NO)
  1315. {
  1316. [self restorePosition];
  1317. _topViewBeginOrigin = kIQCGPointInvalid;
  1318. }
  1319. else
  1320. {
  1321. if (CGPointEqualToPoint(_topViewBeginOrigin, kIQCGPointInvalid)) // (Bug ID: #5)
  1322. {
  1323. // keyboard is not showing(At the beginning only).
  1324. UIViewController *rootController = [textFieldView parentContainerViewController];
  1325. _rootViewController = rootController;
  1326. if (_rootViewControllerWhilePopGestureRecognizerActive == rootController)
  1327. {
  1328. _topViewBeginOrigin = _topViewBeginOriginWhilePopGestureRecognizerActive;
  1329. }
  1330. else
  1331. {
  1332. _topViewBeginOrigin = rootController.view.frame.origin;
  1333. }
  1334. _rootViewControllerWhilePopGestureRecognizerActive = nil;
  1335. _topViewBeginOriginWhilePopGestureRecognizerActive = kIQCGPointInvalid;
  1336. [self showLog:[NSString stringWithFormat:@"Saving %@ beginning origin: %@",rootController, NSStringFromCGPoint(_topViewBeginOrigin)]];
  1337. }
  1338. //If textFieldView is inside AlertView then do nothing. (Bug ID: #37, #74, #76)
  1339. //See notes:- https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html If it is AlertView textField then do not affect anything (Bug ID: #70).
  1340. if (_keyboardShowing == YES &&
  1341. textFieldView &&
  1342. [textFieldView isAlertViewTextField] == NO)
  1343. {
  1344. // keyboard is already showing. adjust frame.
  1345. [self optimizedAdjustPosition];
  1346. }
  1347. }
  1348. // if ([textFieldView isKindOfClass:[UITextField class]])
  1349. // {
  1350. // [(UITextField*)textFieldView addTarget:self action:@selector(editingDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
  1351. // }
  1352. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1353. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1354. }
  1355. /** UITextFieldTextDidEndEditingNotification, UITextViewTextDidEndEditingNotification. Removing fetched object. */
  1356. -(void)textFieldViewDidEndEditing:(NSNotification*)notification
  1357. {
  1358. UIView *object = (UIView*)notification.object;
  1359. if (object.window.isKeyWindow == NO) {
  1360. return;
  1361. }
  1362. CFTimeInterval startTime = CACurrentMediaTime();
  1363. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1364. [self showLog:[NSString stringWithFormat:@"Notification Object: %@", notification.object]];
  1365. UIView *textFieldView = _textFieldView;
  1366. //Removing gesture recognizer (Enhancement ID: #14)
  1367. [textFieldView.window removeGestureRecognizer:_resignFirstResponderGesture];
  1368. // if ([textFieldView isKindOfClass:[UITextField class]])
  1369. // {
  1370. // [(UITextField*)textFieldView removeTarget:self action:@selector(editingDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
  1371. // }
  1372. // We check if there's a change in original frame or not.
  1373. if(_isTextViewContentInsetChanged == YES &&
  1374. [textFieldView respondsToSelector:@selector(isEditable)] && [textFieldView isKindOfClass:[UIScrollView class]])
  1375. {
  1376. UIScrollView *textView = (UIScrollView*)textFieldView;
  1377. self.isTextViewContentInsetChanged = NO;
  1378. if (UIEdgeInsetsEqualToEdgeInsets(textView.contentInset, self.startingTextViewContentInsets) == NO)
  1379. {
  1380. __weak __typeof__(self) weakSelf = self;
  1381. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  1382. __strong __typeof__(self) strongSelf = weakSelf;
  1383. [strongSelf showLog:[NSString stringWithFormat:@"Restoring textView.contentInset to : %@",NSStringFromUIEdgeInsets(strongSelf.startingTextViewContentInsets)]];
  1384. //Setting textField to it's initial contentInset
  1385. textView.contentInset = strongSelf.startingTextViewContentInsets;
  1386. textView.scrollIndicatorInsets = strongSelf.startingTextViewScrollIndicatorInsets;
  1387. } completion:NULL];
  1388. }
  1389. }
  1390. //Setting object to nil
  1391. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160000
  1392. if (@available(iOS 16.0, *)) {
  1393. if ([textFieldView isKindOfClass:[UITextView class]] && [(UITextView*)textFieldView isFindInteractionEnabled]) {
  1394. //Not setting it nil, because it may be doing find interaction.
  1395. //As of now, here textView.findInteraction.isFindNavigatorVisible returns NO
  1396. //So there is no way to detect if this is dismissed due to findInteraction
  1397. } else {
  1398. textFieldView = nil;
  1399. }
  1400. } else
  1401. #endif
  1402. {
  1403. textFieldView = nil;
  1404. }
  1405. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1406. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1407. }
  1408. //-(void)editingDidEndOnExit:(UITextField*)textField
  1409. //{
  1410. // [self showLog:[NSString stringWithFormat:@"ReturnKey %@",NSStringFromSelector(_cmd)]];
  1411. //}
  1412. #pragma mark - UIStatusBar Notification methods
  1413. /** UIApplicationWillChangeStatusBarOrientationNotification. Need to set the textView to it's original position. If any frame changes made. (Bug ID: #92)*/
  1414. - (void)willChangeStatusBarOrientation:(NSNotification*)aNotification
  1415. {
  1416. UIInterfaceOrientation currentStatusBarOrientation = UIInterfaceOrientationUnknown;
  1417. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  1418. if (@available(iOS 13.0, *)) {
  1419. currentStatusBarOrientation = [self keyWindow].windowScene.interfaceOrientation;
  1420. } else
  1421. #endif
  1422. {
  1423. #if __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
  1424. currentStatusBarOrientation = UIApplication.sharedApplication.statusBarOrientation;
  1425. #endif
  1426. }
  1427. #pragma clang diagnostic push
  1428. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1429. UIInterfaceOrientation statusBarOrientation = [aNotification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
  1430. #pragma clang diagnostic pop
  1431. if (statusBarOrientation != currentStatusBarOrientation) {
  1432. return;
  1433. }
  1434. CFTimeInterval startTime = CACurrentMediaTime();
  1435. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1436. [self showLog:[NSString stringWithFormat:@"Notification Object: %@", aNotification.object]];
  1437. __strong __typeof__(UIView) *strongTextFieldView = _textFieldView;
  1438. //If textViewContentInsetChanged is changed then restore it.
  1439. if (_isTextViewContentInsetChanged == YES &&
  1440. [strongTextFieldView respondsToSelector:@selector(isEditable)] && [strongTextFieldView isKindOfClass:[UIScrollView class]])
  1441. {
  1442. UIScrollView *textView = (UIScrollView*)strongTextFieldView;
  1443. self.isTextViewContentInsetChanged = NO;
  1444. if (UIEdgeInsetsEqualToEdgeInsets(textView.contentInset, self.startingTextViewContentInsets) == NO)
  1445. {
  1446. __weak __typeof__(self) weakSelf = self;
  1447. //Due to orientation callback we need to set it's original position.
  1448. [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve|UIViewAnimationOptionBeginFromCurrentState) animations:^{
  1449. __strong __typeof__(self) strongSelf = weakSelf;
  1450. [strongSelf showLog:[NSString stringWithFormat:@"Restoring textView.contentInset to : %@",NSStringFromUIEdgeInsets(strongSelf.startingTextViewContentInsets)]];
  1451. //Setting textField to it's initial contentInset
  1452. textView.contentInset = strongSelf.startingTextViewContentInsets;
  1453. textView.scrollIndicatorInsets = strongSelf.startingTextViewScrollIndicatorInsets;
  1454. } completion:NULL];
  1455. }
  1456. }
  1457. [self restorePosition];
  1458. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1459. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1460. }
  1461. #pragma mark AutoResign methods
  1462. /** Resigning on tap gesture. */
  1463. - (void)tapRecognized:(UITapGestureRecognizer*)gesture // (Enhancement ID: #14)
  1464. {
  1465. if (gesture.state == UIGestureRecognizerStateEnded)
  1466. {
  1467. //Resigning currently responder textField.
  1468. [self resignFirstResponder];
  1469. }
  1470. }
  1471. /** Note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES. */
  1472. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  1473. {
  1474. return NO;
  1475. }
  1476. /** To not detect touch events in a subclass of UIControl, these may have added their own selector for specific work */
  1477. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  1478. {
  1479. // Should not recognize gesture if the clicked view is either UIControl or UINavigationBar(<Back button etc...) (Bug ID: #145)
  1480. for (Class aClass in self.touchResignedGestureIgnoreClasses)
  1481. {
  1482. if ([[touch view] isKindOfClass:aClass])
  1483. {
  1484. return NO;
  1485. }
  1486. }
  1487. return YES;
  1488. }
  1489. /** Resigning textField. */
  1490. - (BOOL)resignFirstResponder
  1491. {
  1492. UIView *textFieldView = _textFieldView;
  1493. if (textFieldView)
  1494. {
  1495. // Retaining textFieldView
  1496. UIView *textFieldRetain = textFieldView;
  1497. //Resigning first responder
  1498. BOOL isResignFirstResponder = [textFieldView resignFirstResponder];
  1499. // If it refuses then becoming it as first responder again. (Bug ID: #96)
  1500. if (isResignFirstResponder == NO)
  1501. {
  1502. //If it refuses to resign then becoming it first responder again for getting notifications callback.
  1503. [textFieldRetain becomeFirstResponder];
  1504. [self showLog:[NSString stringWithFormat:@"Refuses to Resign first responder: %@",textFieldView]];
  1505. }
  1506. return isResignFirstResponder;
  1507. }
  1508. else
  1509. {
  1510. return NO;
  1511. }
  1512. }
  1513. /** Returns YES if can navigate to previous responder textField/textView, otherwise NO. */
  1514. -(BOOL)canGoPrevious
  1515. {
  1516. //Getting all responder view's.
  1517. NSArray<UIView*> *textFields = [self responderViews];
  1518. //Getting index of current textField.
  1519. NSUInteger index = [textFields indexOfObject:_textFieldView];
  1520. //If it is not first textField. then it's previous object can becomeFirstResponder.
  1521. if (index != NSNotFound &&
  1522. index > 0)
  1523. {
  1524. return YES;
  1525. }
  1526. else
  1527. {
  1528. return NO;
  1529. }
  1530. }
  1531. /** Returns YES if can navigate to next responder textField/textView, otherwise NO. */
  1532. -(BOOL)canGoNext
  1533. {
  1534. //Getting all responder view's.
  1535. NSArray<UIView*> *textFields = [self responderViews];
  1536. //Getting index of current textField.
  1537. NSUInteger index = [textFields indexOfObject:_textFieldView];
  1538. //If it is not last textField. then it's next object becomeFirstResponder.
  1539. if (index != NSNotFound &&
  1540. index < textFields.count-1)
  1541. {
  1542. return YES;
  1543. }
  1544. else
  1545. {
  1546. return NO;
  1547. }
  1548. }
  1549. /** Navigate to previous responder textField/textView. */
  1550. -(BOOL)goPrevious
  1551. {
  1552. //Getting all responder view's.
  1553. NSArray<__kindof UIView*> *textFields = [self responderViews];
  1554. //Getting index of current textField.
  1555. NSUInteger index = [textFields indexOfObject:_textFieldView];
  1556. //If it is not first textField. then it's previous object becomeFirstResponder.
  1557. if (index != NSNotFound &&
  1558. index > 0)
  1559. {
  1560. UITextField *nextTextField = textFields[index-1];
  1561. BOOL isAcceptAsFirstResponder = [nextTextField becomeFirstResponder];
  1562. // If it refuses then becoming previous textFieldView as first responder again. (Bug ID: #96)
  1563. if (isAcceptAsFirstResponder == NO)
  1564. {
  1565. [self showLog:[NSString stringWithFormat:@"Refuses to become first responder: %@",nextTextField]];
  1566. }
  1567. return isAcceptAsFirstResponder;
  1568. }
  1569. else
  1570. {
  1571. return NO;
  1572. }
  1573. }
  1574. /** Navigate to next responder textField/textView. */
  1575. -(BOOL)goNext
  1576. {
  1577. //Getting all responder view's.
  1578. NSArray<__kindof UIView*> *textFields = [self responderViews];
  1579. //Getting index of current textField.
  1580. NSUInteger index = [textFields indexOfObject:_textFieldView];
  1581. //If it is not last textField. then it's next object becomeFirstResponder.
  1582. if (index != NSNotFound &&
  1583. index < textFields.count-1)
  1584. {
  1585. UITextField *nextTextField = textFields[index+1];
  1586. BOOL isAcceptAsFirstResponder = [nextTextField becomeFirstResponder];
  1587. // If it refuses then becoming previous textFieldView as first responder again. (Bug ID: #96)
  1588. if (isAcceptAsFirstResponder == NO)
  1589. {
  1590. [self showLog:[NSString stringWithFormat:@"Refuses to become first responder: %@",nextTextField]];
  1591. }
  1592. return isAcceptAsFirstResponder;
  1593. }
  1594. else
  1595. {
  1596. return NO;
  1597. }
  1598. }
  1599. #pragma mark AutoToolbar methods
  1600. /** Get all UITextField/UITextView siblings of textFieldView. */
  1601. -(NSArray<__kindof UIView*>*)responderViews
  1602. {
  1603. UIView *superConsideredView;
  1604. UIView *textFieldView = _textFieldView;
  1605. //If find any consider responderView in it's upper hierarchy then will get deepResponderView.
  1606. for (Class consideredClass in _toolbarPreviousNextAllowedClasses)
  1607. {
  1608. superConsideredView = [textFieldView superviewOfClassType:consideredClass];
  1609. if (superConsideredView)
  1610. break;
  1611. }
  1612. //If there is a superConsideredView in view's hierarchy, then fetching all it's subview that responds. No sorting for superConsideredView, it's by subView position. (Enhancement ID: #22)
  1613. if (superConsideredView)
  1614. {
  1615. return [superConsideredView deepResponderViews];
  1616. }
  1617. //Otherwise fetching all the siblings
  1618. else
  1619. {
  1620. NSArray<UIView*> *textFields = [textFieldView responderSiblings];
  1621. //Sorting textFields according to behaviour
  1622. switch (_toolbarManageBehaviour)
  1623. {
  1624. //If autoToolbar behaviour is bySubviews, then returning it.
  1625. case IQAutoToolbarBySubviews:
  1626. return textFields;
  1627. break;
  1628. //If autoToolbar behaviour is by tag, then sorting it according to tag property.
  1629. case IQAutoToolbarByTag:
  1630. return [textFields sortedArrayByTag];
  1631. break;
  1632. //If autoToolbar behaviour is by tag, then sorting it according to tag property.
  1633. case IQAutoToolbarByPosition:
  1634. return [textFields sortedArrayByPosition];
  1635. break;
  1636. default:
  1637. return nil;
  1638. break;
  1639. }
  1640. }
  1641. }
  1642. /** Add toolbar if it is required to add on textFields and it's siblings. */
  1643. -(void)addToolbarIfRequired
  1644. {
  1645. CFTimeInterval startTime = CACurrentMediaTime();
  1646. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1647. // Getting all the sibling textFields.
  1648. NSArray<UIView*> *siblings = [self responderViews];
  1649. [self showLog:[NSString stringWithFormat:@"Found %lu responder sibling(s)",(unsigned long)siblings.count]];
  1650. UIView *textFieldView = _textFieldView;
  1651. //Either there is no inputAccessoryView or if accessoryView is not appropriate for current situation(There is Previous/Next/Done toolbar).
  1652. //setInputAccessoryView: check (Bug ID: #307)
  1653. if ([textFieldView respondsToSelector:@selector(setInputAccessoryView:)])
  1654. {
  1655. if ([textFieldView inputAccessoryView] == nil ||
  1656. [[textFieldView inputAccessoryView] tag] == kIQPreviousNextButtonToolbarTag ||
  1657. [[textFieldView inputAccessoryView] tag] == kIQDoneButtonToolbarTag)
  1658. {
  1659. UITextField *textField = (UITextField*)textFieldView;
  1660. IQBarButtonItemConfiguration *rightConfiguration = nil;
  1661. //Supporting Custom Done button image (Enhancement ID: #366)
  1662. if (_toolbarDoneBarButtonItemImage)
  1663. {
  1664. rightConfiguration = [[IQBarButtonItemConfiguration alloc] initWithImage:_toolbarDoneBarButtonItemImage action:@selector(doneAction:)];
  1665. }
  1666. //Supporting Custom Done button text (Enhancement ID: #209, #411, Bug ID: #376)
  1667. else if (_toolbarDoneBarButtonItemText)
  1668. {
  1669. rightConfiguration = [[IQBarButtonItemConfiguration alloc] initWithTitle:_toolbarDoneBarButtonItemText action:@selector(doneAction:)];
  1670. }
  1671. else
  1672. {
  1673. rightConfiguration = [[IQBarButtonItemConfiguration alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone action:@selector(doneAction:)];
  1674. }
  1675. rightConfiguration.accessibilityLabel = _toolbarDoneBarButtonItemAccessibilityLabel ? : @"Done";
  1676. // If only one object is found, then adding only Done button.
  1677. if ((siblings.count <= 1 && self.previousNextDisplayMode == IQPreviousNextDisplayModeDefault) || self.previousNextDisplayMode == IQPreviousNextDisplayModeAlwaysHide)
  1678. {
  1679. [textField addKeyboardToolbarWithTarget:self titleText:(_shouldShowToolbarPlaceholder ? textField.drawingToolbarPlaceholder : nil) rightBarButtonConfiguration:rightConfiguration previousBarButtonConfiguration:nil nextBarButtonConfiguration:nil];
  1680. textField.inputAccessoryView.tag = kIQDoneButtonToolbarTag; // (Bug ID: #78)
  1681. }
  1682. //If there is multiple siblings of textField
  1683. else if ((self.previousNextDisplayMode == IQPreviousNextDisplayModeDefault) || self.previousNextDisplayMode == IQPreviousNextDisplayModeAlwaysShow)
  1684. {
  1685. IQBarButtonItemConfiguration *prevConfiguration = nil;
  1686. //Supporting Custom Done button image (Enhancement ID: #366)
  1687. if (_toolbarPreviousBarButtonItemImage)
  1688. {
  1689. prevConfiguration = [[IQBarButtonItemConfiguration alloc] initWithImage:_toolbarPreviousBarButtonItemImage action:@selector(previousAction:)];
  1690. }
  1691. //Supporting Custom Done button text (Enhancement ID: #209, #411, Bug ID: #376)
  1692. else if (_toolbarPreviousBarButtonItemText)
  1693. {
  1694. prevConfiguration = [[IQBarButtonItemConfiguration alloc] initWithTitle:_toolbarPreviousBarButtonItemText action:@selector(previousAction:)];
  1695. }
  1696. else
  1697. {
  1698. prevConfiguration = [[IQBarButtonItemConfiguration alloc] initWithImage:[UIImage keyboardPreviousImage] action:@selector(previousAction:)];
  1699. }
  1700. prevConfiguration.accessibilityLabel = _toolbarPreviousBarButtonItemAccessibilityLabel ? : @"Previous";
  1701. IQBarButtonItemConfiguration *nextConfiguration = nil;
  1702. //Supporting Custom Done button image (Enhancement ID: #366)
  1703. if (_toolbarNextBarButtonItemImage)
  1704. {
  1705. nextConfiguration = [[IQBarButtonItemConfiguration alloc] initWithImage:_toolbarNextBarButtonItemImage action:@selector(nextAction:)];
  1706. }
  1707. //Supporting Custom Done button text (Enhancement ID: #209, #411, Bug ID: #376)
  1708. else if (_toolbarNextBarButtonItemText)
  1709. {
  1710. nextConfiguration = [[IQBarButtonItemConfiguration alloc] initWithTitle:_toolbarNextBarButtonItemText action:@selector(nextAction:)];
  1711. }
  1712. else
  1713. {
  1714. nextConfiguration = [[IQBarButtonItemConfiguration alloc] initWithImage:[UIImage keyboardNextImage] action:@selector(nextAction:)];
  1715. }
  1716. nextConfiguration.accessibilityLabel = _toolbarNextBarButtonItemAccessibilityLabel ? : @"Next";
  1717. [textField addKeyboardToolbarWithTarget:self titleText:(_shouldShowToolbarPlaceholder ? textField.drawingToolbarPlaceholder : nil) rightBarButtonConfiguration:rightConfiguration previousBarButtonConfiguration:prevConfiguration nextBarButtonConfiguration:nextConfiguration];
  1718. textField.inputAccessoryView.tag = kIQPreviousNextButtonToolbarTag; // (Bug ID: #78)
  1719. }
  1720. IQToolbar *toolbar = textField.keyboardToolbar;
  1721. //Bar style according to keyboard appearance
  1722. if ([textField respondsToSelector:@selector(keyboardAppearance)])
  1723. {
  1724. //Setting toolbar tintColor // (Enhancement ID: #30)
  1725. if (_shouldToolbarUsesTextFieldTintColor)
  1726. {
  1727. toolbar.tintColor = [textField tintColor];
  1728. }
  1729. else if (_toolbarTintColor)
  1730. {
  1731. toolbar.tintColor = _toolbarTintColor;
  1732. }
  1733. else
  1734. {
  1735. toolbar.tintColor = nil;
  1736. }
  1737. switch ([textField keyboardAppearance])
  1738. {
  1739. case UIKeyboardAppearanceDark:
  1740. {
  1741. toolbar.barStyle = UIBarStyleBlack;
  1742. [toolbar setBarTintColor:nil];
  1743. }
  1744. break;
  1745. default:
  1746. {
  1747. toolbar.barStyle = UIBarStyleDefault;
  1748. toolbar.barTintColor = _toolbarBarTintColor;
  1749. }
  1750. break;
  1751. }
  1752. //If need to show placeholder
  1753. if (_shouldShowToolbarPlaceholder &&
  1754. textField.shouldHideToolbarPlaceholder == NO)
  1755. {
  1756. //Updating placeholder //(Bug ID: #148, #272)
  1757. if (toolbar.titleBarButton.title == nil ||
  1758. [toolbar.titleBarButton.title isEqualToString:textField.drawingToolbarPlaceholder] == NO)
  1759. {
  1760. [toolbar.titleBarButton setTitle:textField.drawingToolbarPlaceholder];
  1761. }
  1762. //Setting toolbar title font. // (Enhancement ID: #30)
  1763. if (_placeholderFont &&
  1764. [_placeholderFont isKindOfClass:[UIFont class]])
  1765. {
  1766. [toolbar.titleBarButton setTitleFont:_placeholderFont];
  1767. }
  1768. //Setting toolbar title color. // (Enhancement ID: #880)
  1769. if (_placeholderColor)
  1770. {
  1771. [toolbar.titleBarButton setTitleColor:_placeholderColor];
  1772. }
  1773. //Setting toolbar button title color. // (Enhancement ID: #880)
  1774. if (_placeholderButtonColor)
  1775. {
  1776. [toolbar.titleBarButton setSelectableTitleColor:_placeholderButtonColor];
  1777. }
  1778. }
  1779. else
  1780. {
  1781. //Updating placeholder //(Bug ID: #272)
  1782. toolbar.titleBarButton.title = nil;
  1783. }
  1784. }
  1785. //In case of UITableView (Special), the next/previous buttons has to be refreshed everytime. (Bug ID: #56)
  1786. // If firstTextField, then previous should not be enabled.
  1787. if (siblings.firstObject == textField)
  1788. {
  1789. if (siblings.count == 1)
  1790. {
  1791. textField.keyboardToolbar.previousBarButton.enabled = NO;
  1792. textField.keyboardToolbar.nextBarButton.enabled = NO;
  1793. }
  1794. else
  1795. {
  1796. textField.keyboardToolbar.previousBarButton.enabled = NO;
  1797. textField.keyboardToolbar.nextBarButton.enabled = YES;
  1798. }
  1799. }
  1800. // If lastTextField then next should not be enaled.
  1801. else if ([siblings lastObject] == textField)
  1802. {
  1803. textField.keyboardToolbar.previousBarButton.enabled = YES;
  1804. textField.keyboardToolbar.nextBarButton.enabled = NO;
  1805. }
  1806. else
  1807. {
  1808. textField.keyboardToolbar.previousBarButton.enabled = YES;
  1809. textField.keyboardToolbar.nextBarButton.enabled = YES;
  1810. }
  1811. }
  1812. }
  1813. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1814. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1815. }
  1816. /** Remove any toolbar if it is IQToolbar. */
  1817. -(void)removeToolbarIfRequired // (Bug ID: #18)
  1818. {
  1819. CFTimeInterval startTime = CACurrentMediaTime();
  1820. [self showLog:[NSString stringWithFormat:@">>>>> %@ started >>>>>",NSStringFromSelector(_cmd)] indentation:1];
  1821. // Getting all the sibling textFields.
  1822. NSArray<UIView*> *siblings = [self responderViews];
  1823. [self showLog:[NSString stringWithFormat:@"Found %lu responder sibling(s)",(unsigned long)siblings.count]];
  1824. for (UITextField *textField in siblings)
  1825. {
  1826. UIView *toolbar = [textField inputAccessoryView];
  1827. // (Bug ID: #78)
  1828. //setInputAccessoryView: check (Bug ID: #307)
  1829. if ([textField respondsToSelector:@selector(setInputAccessoryView:)] &&
  1830. ([toolbar isKindOfClass:[IQToolbar class]] && (toolbar.tag == kIQDoneButtonToolbarTag || toolbar.tag == kIQPreviousNextButtonToolbarTag)))
  1831. {
  1832. textField.inputAccessoryView = nil;
  1833. [textField reloadInputViews];
  1834. }
  1835. }
  1836. CFTimeInterval elapsedTime = CACurrentMediaTime() - startTime;
  1837. [self showLog:[NSString stringWithFormat:@"<<<<< %@ ended: %g seconds <<<<<",NSStringFromSelector(_cmd),elapsedTime] indentation:-1];
  1838. }
  1839. /** reloadInputViews to reload toolbar buttons enable/disable state on the fly Enhancement ID #434. */
  1840. - (void)reloadInputViews
  1841. {
  1842. //If enabled then adding toolbar.
  1843. if ([self privateIsEnableAutoToolbar] == YES)
  1844. {
  1845. [self addToolbarIfRequired];
  1846. }
  1847. //Else removing toolbar.
  1848. else
  1849. {
  1850. [self removeToolbarIfRequired];
  1851. }
  1852. }
  1853. #pragma mark previous/next/done functionality
  1854. /** previousAction. */
  1855. -(void)previousAction:(IQBarButtonItem*)barButton
  1856. {
  1857. //If user wants to play input Click sound. Then Play Input Click Sound.
  1858. if (_shouldPlayInputClicks)
  1859. {
  1860. [[UIDevice currentDevice] playInputClick];
  1861. }
  1862. if ([self canGoPrevious])
  1863. {
  1864. UIView *currentTextFieldView = _textFieldView;
  1865. BOOL isAcceptAsFirstResponder = [self goPrevious];
  1866. NSInvocation *invocation = barButton.invocation;
  1867. UIView *sender = currentTextFieldView;
  1868. //Handling search bar special case
  1869. {
  1870. UISearchBar *searchBar = currentTextFieldView.textFieldSearchBar;
  1871. if (searchBar)
  1872. {
  1873. invocation = searchBar.keyboardToolbar.previousBarButton.invocation;
  1874. sender = searchBar;
  1875. }
  1876. }
  1877. if (isAcceptAsFirstResponder == YES && invocation)
  1878. {
  1879. if (invocation.methodSignature.numberOfArguments > 2)
  1880. {
  1881. [invocation setArgument:&sender atIndex:2];
  1882. }
  1883. [invocation invoke];
  1884. }
  1885. }
  1886. }
  1887. /** nextAction. */
  1888. -(void)nextAction:(IQBarButtonItem*)barButton
  1889. {
  1890. //If user wants to play input Click sound. Then Play Input Click Sound.
  1891. if (_shouldPlayInputClicks)
  1892. {
  1893. [[UIDevice currentDevice] playInputClick];
  1894. }
  1895. if ([self canGoNext])
  1896. {
  1897. UIView *currentTextFieldView = _textFieldView;
  1898. BOOL isAcceptAsFirstResponder = [self goNext];
  1899. NSInvocation *invocation = barButton.invocation;
  1900. UIView *sender = currentTextFieldView;
  1901. //Handling search bar special case
  1902. {
  1903. UISearchBar *searchBar = currentTextFieldView.textFieldSearchBar;
  1904. if (searchBar)
  1905. {
  1906. invocation = searchBar.keyboardToolbar.nextBarButton.invocation;
  1907. sender = searchBar;
  1908. }
  1909. }
  1910. if (isAcceptAsFirstResponder == YES && invocation)
  1911. {
  1912. if (invocation.methodSignature.numberOfArguments > 2)
  1913. {
  1914. [invocation setArgument:&sender atIndex:2];
  1915. }
  1916. [invocation invoke];
  1917. }
  1918. }
  1919. }
  1920. /** doneAction. Resigning current textField. */
  1921. -(void)doneAction:(IQBarButtonItem*)barButton
  1922. {
  1923. //If user wants to play input Click sound. Then Play Input Click Sound.
  1924. if (_shouldPlayInputClicks)
  1925. {
  1926. [[UIDevice currentDevice] playInputClick];
  1927. }
  1928. UIView *currentTextFieldView = _textFieldView;
  1929. BOOL isResignedFirstResponder = [self resignFirstResponder];
  1930. NSInvocation *invocation = barButton.invocation;
  1931. UIView *sender = currentTextFieldView;
  1932. //Handling search bar special case
  1933. {
  1934. UISearchBar *searchBar = currentTextFieldView.textFieldSearchBar;
  1935. if (searchBar)
  1936. {
  1937. invocation = searchBar.keyboardToolbar.doneBarButton.invocation;
  1938. sender = searchBar;
  1939. }
  1940. }
  1941. if (isResignedFirstResponder == YES && invocation)
  1942. {
  1943. if (invocation.methodSignature.numberOfArguments > 2)
  1944. {
  1945. [invocation setArgument:&sender atIndex:2];
  1946. }
  1947. [invocation invoke];
  1948. }
  1949. }
  1950. #pragma mark - Customised textField/textView support.
  1951. /**
  1952. Add customised Notification for third party customised TextField/TextView.
  1953. */
  1954. -(void)registerTextFieldViewClass:(nonnull Class)aClass
  1955. didBeginEditingNotificationName:(nonnull NSString *)didBeginEditingNotificationName
  1956. didEndEditingNotificationName:(nonnull NSString *)didEndEditingNotificationName
  1957. {
  1958. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidBeginEditing:) name:didBeginEditingNotificationName object:nil];
  1959. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldViewDidEndEditing:) name:didEndEditingNotificationName object:nil];
  1960. }
  1961. /**
  1962. Remove customised Notification for third party customised TextField/TextView.
  1963. */
  1964. -(void)unregisterTextFieldViewClass:(nonnull Class)aClass
  1965. didBeginEditingNotificationName:(nonnull NSString *)didBeginEditingNotificationName
  1966. didEndEditingNotificationName:(nonnull NSString *)didEndEditingNotificationName
  1967. {
  1968. [[NSNotificationCenter defaultCenter] removeObserver:self name:didBeginEditingNotificationName object:nil];
  1969. [[NSNotificationCenter defaultCenter] removeObserver:self name:didEndEditingNotificationName object:nil];
  1970. }
  1971. -(void)registerAllNotifications
  1972. {
  1973. // Registering for keyboard notification.
  1974. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  1975. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
  1976. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  1977. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
  1978. // Registering for UITextField notification.
  1979. [self registerTextFieldViewClass:[UITextField class]
  1980. didBeginEditingNotificationName:UITextFieldTextDidBeginEditingNotification
  1981. didEndEditingNotificationName:UITextFieldTextDidEndEditingNotification];
  1982. // Registering for UITextView notification.
  1983. [self registerTextFieldViewClass:[UITextView class]
  1984. didBeginEditingNotificationName:UITextViewTextDidBeginEditingNotification
  1985. didEndEditingNotificationName:UITextViewTextDidEndEditingNotification];
  1986. // Registering for orientation changes notification
  1987. #pragma clang diagnostic push
  1988. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1989. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willChangeStatusBarOrientation:) name:UIApplicationWillChangeStatusBarOrientationNotification object:[UIApplication sharedApplication]];
  1990. #pragma clang diagnostic pop
  1991. }
  1992. -(void)unregisterAllNotifications
  1993. {
  1994. // Unregistering for keyboard notification.
  1995. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  1996. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
  1997. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  1998. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
  1999. // Unregistering for UITextField notification.
  2000. [self unregisterTextFieldViewClass:[UITextField class]
  2001. didBeginEditingNotificationName:UITextFieldTextDidBeginEditingNotification
  2002. didEndEditingNotificationName:UITextFieldTextDidEndEditingNotification];
  2003. // Unregistering for UITextView notification.
  2004. [self unregisterTextFieldViewClass:[UITextView class]
  2005. didBeginEditingNotificationName:UITextViewTextDidBeginEditingNotification
  2006. didEndEditingNotificationName:UITextViewTextDidEndEditingNotification];
  2007. // Unregistering for orientation changes notification
  2008. #pragma clang diagnostic push
  2009. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  2010. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:[UIApplication sharedApplication]];
  2011. #pragma clang diagnostic pop
  2012. }
  2013. -(void)showLog:(NSString*)logString
  2014. {
  2015. [self showLog:logString indentation:0];
  2016. }
  2017. -(void)showLog:(NSString*)logString indentation:(NSInteger)indent
  2018. {
  2019. static NSInteger indentation = 0;
  2020. if (indent < 0)
  2021. {
  2022. indentation = MAX(0, indentation + indent);
  2023. }
  2024. if (_enableDebugging)
  2025. {
  2026. NSMutableString *preLog = [[NSMutableString alloc] init];
  2027. for (int i = 0; i<=indentation; i++) {
  2028. [preLog appendString:@"|\t"];
  2029. }
  2030. [preLog appendString:logString];
  2031. NSLog(@"%@",preLog);
  2032. }
  2033. if (indent > 0)
  2034. {
  2035. indentation += indent;
  2036. }
  2037. }
  2038. @end