IQKeyboardManager.swift 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. //
  2. // IQKeyboardManager.swift
  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 Foundation
  24. import CoreGraphics
  25. import UIKit
  26. import QuartzCore
  27. ///---------------------
  28. /// MARK: IQToolbar tags
  29. ///---------------------
  30. /**
  31. Codeless drop-in universal library allows to prevent issues of keyboard sliding up and cover UITextField/UITextView. Neither need to write any code nor any setup required and much more. A generic version of KeyboardManagement. https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html
  32. */
  33. @objc public class IQKeyboardManager: NSObject, UIGestureRecognizerDelegate {
  34. /**
  35. Default tag for toolbar with Done button -1002.
  36. */
  37. private static let kIQDoneButtonToolbarTag = -1002
  38. /**
  39. Default tag for toolbar with Previous/Next buttons -1005.
  40. */
  41. private static let kIQPreviousNextButtonToolbarTag = -1005
  42. /**
  43. Invalid point value.
  44. */
  45. private static let kIQCGPointInvalid = CGPoint.init(x: CGFloat.greatestFiniteMagnitude, y: CGFloat.greatestFiniteMagnitude)
  46. ///---------------------------
  47. /// MARK: UIKeyboard handling
  48. ///---------------------------
  49. /**
  50. Enable/disable managing distance between keyboard and textField. Default is YES(Enabled when class loads in `+(void)load` method).
  51. */
  52. @objc public var enable = false {
  53. didSet {
  54. //If not enable, enable it.
  55. if enable == true &&
  56. oldValue == false {
  57. //If keyboard is currently showing. Sending a fake notification for keyboardWillHide to retain view's original position.
  58. if let notification = _kbShowNotification {
  59. keyboardWillShow(notification)
  60. }
  61. showLog("Enabled")
  62. } else if enable == false &&
  63. oldValue == true { //If not disable, desable it.
  64. keyboardWillHide(nil)
  65. showLog("Disabled")
  66. }
  67. }
  68. }
  69. private func privateIsEnabled() -> Bool {
  70. var isEnabled = enable
  71. let enableMode = _textFieldView?.enableMode
  72. if enableMode == .enabled {
  73. isEnabled = true
  74. } else if enableMode == .disabled {
  75. isEnabled = false
  76. } else {
  77. if var textFieldViewController = _textFieldView?.viewContainingController() {
  78. //If it is searchBar textField embedded in Navigation Bar
  79. if _textFieldView?.textFieldSearchBar() != nil, let navController = textFieldViewController as? UINavigationController, let topController = navController.topViewController {
  80. textFieldViewController = topController
  81. }
  82. if isEnabled == false {
  83. //If viewController is kind of enable viewController class, then assuming it's enabled.
  84. for enabledClass in enabledDistanceHandlingClasses {
  85. if textFieldViewController.isKind(of: enabledClass) {
  86. isEnabled = true
  87. break
  88. }
  89. }
  90. }
  91. if isEnabled == true {
  92. //If viewController is kind of disabled viewController class, then assuming it's disabled.
  93. for disabledClass in disabledDistanceHandlingClasses {
  94. if textFieldViewController.isKind(of: disabledClass) {
  95. isEnabled = false
  96. break
  97. }
  98. }
  99. //Special Controllers
  100. if isEnabled == true {
  101. let classNameString = NSStringFromClass(type(of: textFieldViewController.self))
  102. //_UIAlertControllerTextFieldViewController
  103. if classNameString.contains("UIAlertController") && classNameString.hasSuffix("TextFieldViewController") {
  104. isEnabled = false
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return isEnabled
  111. }
  112. /**
  113. To set keyboard distance from textField. can't be less than zero. Default is 10.0.
  114. */
  115. @objc public var keyboardDistanceFromTextField: CGFloat {
  116. set {
  117. _privateKeyboardDistanceFromTextField = max(0, newValue)
  118. showLog("keyboardDistanceFromTextField: \(_privateKeyboardDistanceFromTextField)")
  119. }
  120. get {
  121. return _privateKeyboardDistanceFromTextField
  122. }
  123. }
  124. /**
  125. Boolean to know if keyboard is showing.
  126. */
  127. @objc public var keyboardShowing: Bool {
  128. return _privateIsKeyboardShowing
  129. }
  130. /**
  131. moved distance to the top used to maintain distance between keyboard and textField. Most of the time this will be a positive value.
  132. */
  133. @objc public var movedDistance: CGFloat {
  134. return _privateMovedDistance
  135. }
  136. /**
  137. Will be called then movedDistance will be changed
  138. */
  139. @objc public var movedDistanceChanged: ((CGFloat) -> Void)?
  140. /**
  141. Returns the default singleton instance.
  142. */
  143. @objc public class var shared: IQKeyboardManager {
  144. struct Static {
  145. //Singleton instance. Initializing keyboard manger.
  146. static let kbManager = IQKeyboardManager()
  147. }
  148. /** @return Returns the default singleton instance. */
  149. return Static.kbManager
  150. }
  151. ///-------------------------
  152. /// MARK: IQToolbar handling
  153. ///-------------------------
  154. /**
  155. Automatic add the IQToolbar functionality. Default is YES.
  156. */
  157. @objc public var enableAutoToolbar = true {
  158. didSet {
  159. privateIsEnableAutoToolbar() ? addToolbarIfRequired() : removeToolbarIfRequired()
  160. let enableToolbar = enableAutoToolbar ? "Yes" : "NO"
  161. showLog("enableAutoToolbar: \(enableToolbar)")
  162. }
  163. }
  164. private func privateIsEnableAutoToolbar() -> Bool {
  165. var enableToolbar = enableAutoToolbar
  166. if var textFieldViewController = _textFieldView?.viewContainingController() {
  167. //If it is searchBar textField embedded in Navigation Bar
  168. if _textFieldView?.textFieldSearchBar() != nil, let navController = textFieldViewController as? UINavigationController, let topController = navController.topViewController {
  169. textFieldViewController = topController
  170. }
  171. if enableToolbar == false {
  172. //If found any toolbar enabled classes then return.
  173. for enabledClass in enabledToolbarClasses {
  174. if textFieldViewController.isKind(of: enabledClass) {
  175. enableToolbar = true
  176. break
  177. }
  178. }
  179. }
  180. if enableToolbar == true {
  181. //If found any toolbar disabled classes then return.
  182. for disabledClass in disabledToolbarClasses {
  183. if textFieldViewController.isKind(of: disabledClass) {
  184. enableToolbar = false
  185. break
  186. }
  187. }
  188. //Special Controllers
  189. if enableToolbar == true {
  190. let classNameString = NSStringFromClass(type(of: textFieldViewController.self))
  191. //_UIAlertControllerTextFieldViewController
  192. if classNameString.contains("UIAlertController") && classNameString.hasSuffix("TextFieldViewController") {
  193. enableToolbar = false
  194. }
  195. }
  196. }
  197. }
  198. return enableToolbar
  199. }
  200. /**
  201. /**
  202. IQAutoToolbarBySubviews: Creates Toolbar according to subview's hirarchy of Textfield's in view.
  203. IQAutoToolbarByTag: Creates Toolbar according to tag property of TextField's.
  204. IQAutoToolbarByPosition: Creates Toolbar according to the y,x position of textField in it's superview coordinate.
  205. Default is IQAutoToolbarBySubviews.
  206. */
  207. AutoToolbar managing behaviour. Default is IQAutoToolbarBySubviews.
  208. */
  209. @objc public var toolbarManageBehaviour = IQAutoToolbarManageBehaviour.bySubviews
  210. /**
  211. If YES, then uses textField's tintColor property for IQToolbar, otherwise tint color is default. Default is NO.
  212. */
  213. @objc public var shouldToolbarUsesTextFieldTintColor = false
  214. /**
  215. This is used for toolbar.tintColor when textfield.keyboardAppearance is UIKeyboardAppearanceDefault. If shouldToolbarUsesTextFieldTintColor is YES then this property is ignored. Default is nil and uses black color.
  216. */
  217. @objc public var toolbarTintColor: UIColor?
  218. /**
  219. This is used for toolbar.barTintColor. Default is nil.
  220. */
  221. @objc public var toolbarBarTintColor: UIColor?
  222. /**
  223. IQPreviousNextDisplayModeDefault: Show NextPrevious when there are more than 1 textField otherwise hide.
  224. IQPreviousNextDisplayModeAlwaysHide: Do not show NextPrevious buttons in any case.
  225. IQPreviousNextDisplayModeAlwaysShow: Always show nextPrevious buttons, if there are more than 1 textField then both buttons will be visible but will be shown as disabled.
  226. */
  227. @objc public var previousNextDisplayMode = IQPreviousNextDisplayMode.default
  228. /**
  229. Toolbar previous/next/done button icon, If nothing is provided then check toolbarDoneBarButtonItemText to draw done button.
  230. */
  231. @objc public var toolbarPreviousBarButtonItemImage: UIImage?
  232. @objc public var toolbarNextBarButtonItemImage: UIImage?
  233. @objc public var toolbarDoneBarButtonItemImage: UIImage?
  234. /**
  235. Toolbar previous/next/done button text, If nothing is provided then system default 'UIBarButtonSystemItemDone' will be used.
  236. */
  237. @objc public var toolbarPreviousBarButtonItemText: String?
  238. @objc public var toolbarPreviousBarButtonItemAccessibilityLabel: String?
  239. @objc public var toolbarNextBarButtonItemText: String?
  240. @objc public var toolbarNextBarButtonItemAccessibilityLabel: String?
  241. @objc public var toolbarDoneBarButtonItemText: String?
  242. @objc public var toolbarDoneBarButtonItemAccessibilityLabel: String?
  243. /**
  244. If YES, then it add the textField's placeholder text on IQToolbar. Default is YES.
  245. */
  246. @objc public var shouldShowToolbarPlaceholder = true
  247. /**
  248. Placeholder Font. Default is nil.
  249. */
  250. @objc public var placeholderFont: UIFont?
  251. /**
  252. Placeholder Color. Default is nil. Which means lightGray
  253. */
  254. @objc public var placeholderColor: UIColor?
  255. /**
  256. Placeholder Button Color when it's treated as button. Default is nil.
  257. */
  258. @objc public var placeholderButtonColor: UIColor?
  259. ///--------------------------
  260. /// MARK: UITextView handling
  261. ///--------------------------
  262. /** used to adjust contentInset of UITextView. */
  263. private var startingTextViewContentInsets = UIEdgeInsets()
  264. /** used to adjust scrollIndicatorInsets of UITextView. */
  265. private var startingTextViewScrollIndicatorInsets = UIEdgeInsets()
  266. /** used with textView to detect a textFieldView contentInset is changed or not. (Bug ID: #92)*/
  267. private var isTextViewContentInsetChanged = false
  268. ///---------------------------------------
  269. /// MARK: UIKeyboard appearance overriding
  270. ///---------------------------------------
  271. /**
  272. Override the keyboardAppearance for all textField/textView. Default is NO.
  273. */
  274. @objc public var overrideKeyboardAppearance = false
  275. /**
  276. If overrideKeyboardAppearance is YES, then all the textField keyboardAppearance is set using this property.
  277. */
  278. @objc public var keyboardAppearance = UIKeyboardAppearance.default
  279. ///-----------------------------------------------------------
  280. /// MARK: UITextField/UITextView Next/Previous/Resign handling
  281. ///-----------------------------------------------------------
  282. /**
  283. Resigns Keyboard on touching outside of UITextField/View. Default is NO.
  284. */
  285. @objc public var shouldResignOnTouchOutside = false {
  286. didSet {
  287. resignFirstResponderGesture.isEnabled = privateShouldResignOnTouchOutside()
  288. let shouldResign = shouldResignOnTouchOutside ? "Yes" : "NO"
  289. showLog("shouldResignOnTouchOutside: \(shouldResign)")
  290. }
  291. }
  292. /** 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 */
  293. @objc lazy public var resignFirstResponderGesture: UITapGestureRecognizer = {
  294. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapRecognized(_:)))
  295. tapGesture.cancelsTouchesInView = false
  296. tapGesture.delegate = self
  297. return tapGesture
  298. }()
  299. /*******************************************/
  300. private func privateShouldResignOnTouchOutside() -> Bool {
  301. var shouldResign = shouldResignOnTouchOutside
  302. let enableMode = _textFieldView?.shouldResignOnTouchOutsideMode
  303. if enableMode == .enabled {
  304. shouldResign = true
  305. } else if enableMode == .disabled {
  306. shouldResign = false
  307. } else {
  308. if var textFieldViewController = _textFieldView?.viewContainingController() {
  309. //If it is searchBar textField embedded in Navigation Bar
  310. if _textFieldView?.textFieldSearchBar() != nil, let navController = textFieldViewController as? UINavigationController, let topController = navController.topViewController {
  311. textFieldViewController = topController
  312. }
  313. if shouldResign == false {
  314. //If viewController is kind of enable viewController class, then assuming shouldResignOnTouchOutside is enabled.
  315. for enabledClass in enabledTouchResignedClasses {
  316. if textFieldViewController.isKind(of: enabledClass) {
  317. shouldResign = true
  318. break
  319. }
  320. }
  321. }
  322. if shouldResign == true {
  323. //If viewController is kind of disable viewController class, then assuming shouldResignOnTouchOutside is disable.
  324. for disabledClass in disabledTouchResignedClasses {
  325. if textFieldViewController.isKind(of: disabledClass) {
  326. shouldResign = false
  327. break
  328. }
  329. }
  330. //Special Controllers
  331. if shouldResign == true {
  332. let classNameString = NSStringFromClass(type(of: textFieldViewController.self))
  333. //_UIAlertControllerTextFieldViewController
  334. if classNameString.contains("UIAlertController") && classNameString.hasSuffix("TextFieldViewController") {
  335. shouldResign = false
  336. }
  337. }
  338. }
  339. }
  340. }
  341. return shouldResign
  342. }
  343. /**
  344. Resigns currently first responder field.
  345. */
  346. @objc @discardableResult public func resignFirstResponder() -> Bool {
  347. if let textFieldRetain = _textFieldView {
  348. //Resigning first responder
  349. let isResignFirstResponder = textFieldRetain.resignFirstResponder()
  350. // If it refuses then becoming it as first responder again. (Bug ID: #96)
  351. if isResignFirstResponder == false {
  352. //If it refuses to resign then becoming it first responder again for getting notifications callback.
  353. textFieldRetain.becomeFirstResponder()
  354. showLog("Refuses to resign first responder: \(textFieldRetain)")
  355. }
  356. return isResignFirstResponder
  357. }
  358. return false
  359. }
  360. /**
  361. Returns YES if can navigate to previous responder textField/textView, otherwise NO.
  362. */
  363. @objc public var canGoPrevious: Bool {
  364. //Getting all responder view's.
  365. if let textFields = responderViews() {
  366. if let textFieldRetain = _textFieldView {
  367. //Getting index of current textField.
  368. if let index = textFields.firstIndex(of: textFieldRetain) {
  369. //If it is not first textField. then it's previous object canBecomeFirstResponder.
  370. if index > 0 {
  371. return true
  372. }
  373. }
  374. }
  375. }
  376. return false
  377. }
  378. /**
  379. Returns YES if can navigate to next responder textField/textView, otherwise NO.
  380. */
  381. @objc public var canGoNext: Bool {
  382. //Getting all responder view's.
  383. if let textFields = responderViews() {
  384. if let textFieldRetain = _textFieldView {
  385. //Getting index of current textField.
  386. if let index = textFields.firstIndex(of: textFieldRetain) {
  387. //If it is not first textField. then it's previous object canBecomeFirstResponder.
  388. if index < textFields.count-1 {
  389. return true
  390. }
  391. }
  392. }
  393. }
  394. return false
  395. }
  396. /**
  397. Navigate to previous responder textField/textView.
  398. */
  399. @objc @discardableResult public func goPrevious() -> Bool {
  400. //Getting all responder view's.
  401. if let textFieldRetain = _textFieldView {
  402. if let textFields = responderViews() {
  403. //Getting index of current textField.
  404. if let index = textFields.firstIndex(of: textFieldRetain) {
  405. //If it is not first textField. then it's previous object becomeFirstResponder.
  406. if index > 0 {
  407. let nextTextField = textFields[index-1]
  408. let isAcceptAsFirstResponder = nextTextField.becomeFirstResponder()
  409. // If it refuses then becoming previous textFieldView as first responder again. (Bug ID: #96)
  410. if isAcceptAsFirstResponder == false {
  411. //If next field refuses to become first responder then restoring old textField as first responder.
  412. textFieldRetain.becomeFirstResponder()
  413. showLog("Refuses to become first responder: \(nextTextField)")
  414. }
  415. return isAcceptAsFirstResponder
  416. }
  417. }
  418. }
  419. }
  420. return false
  421. }
  422. /**
  423. Navigate to next responder textField/textView.
  424. */
  425. @objc @discardableResult public func goNext() -> Bool {
  426. //Getting all responder view's.
  427. if let textFieldRetain = _textFieldView {
  428. if let textFields = responderViews() {
  429. //Getting index of current textField.
  430. if let index = textFields.firstIndex(of: textFieldRetain) {
  431. //If it is not last textField. then it's next object becomeFirstResponder.
  432. if index < textFields.count-1 {
  433. let nextTextField = textFields[index+1]
  434. let isAcceptAsFirstResponder = nextTextField.becomeFirstResponder()
  435. // If it refuses then becoming previous textFieldView as first responder again. (Bug ID: #96)
  436. if isAcceptAsFirstResponder == false {
  437. //If next field refuses to become first responder then restoring old textField as first responder.
  438. textFieldRetain.becomeFirstResponder()
  439. showLog("Refuses to become first responder: \(nextTextField)")
  440. }
  441. return isAcceptAsFirstResponder
  442. }
  443. }
  444. }
  445. }
  446. return false
  447. }
  448. /** previousAction. */
  449. @objc internal func previousAction (_ barButton: IQBarButtonItem) {
  450. //If user wants to play input Click sound.
  451. if shouldPlayInputClicks == true {
  452. //Play Input Click Sound.
  453. UIDevice.current.playInputClick()
  454. }
  455. if canGoPrevious == true {
  456. if let textFieldRetain = _textFieldView {
  457. let isAcceptAsFirstResponder = goPrevious()
  458. var invocation = barButton.invocation
  459. var sender = textFieldRetain
  460. //Handling search bar special case
  461. do {
  462. if let searchBar = textFieldRetain.textFieldSearchBar() {
  463. invocation = searchBar.keyboardToolbar.previousBarButton.invocation
  464. sender = searchBar
  465. }
  466. }
  467. if isAcceptAsFirstResponder {
  468. invocation?.invoke(from: sender)
  469. }
  470. }
  471. }
  472. }
  473. /** nextAction. */
  474. @objc internal func nextAction (_ barButton: IQBarButtonItem) {
  475. //If user wants to play input Click sound.
  476. if shouldPlayInputClicks == true {
  477. //Play Input Click Sound.
  478. UIDevice.current.playInputClick()
  479. }
  480. if canGoNext == true {
  481. if let textFieldRetain = _textFieldView {
  482. let isAcceptAsFirstResponder = goNext()
  483. var invocation = barButton.invocation
  484. var sender = textFieldRetain
  485. //Handling search bar special case
  486. do {
  487. if let searchBar = textFieldRetain.textFieldSearchBar() {
  488. invocation = searchBar.keyboardToolbar.nextBarButton.invocation
  489. sender = searchBar
  490. }
  491. }
  492. if isAcceptAsFirstResponder {
  493. invocation?.invoke(from: sender)
  494. }
  495. }
  496. }
  497. }
  498. /** doneAction. Resigning current textField. */
  499. @objc internal func doneAction (_ barButton: IQBarButtonItem) {
  500. //If user wants to play input Click sound.
  501. if shouldPlayInputClicks == true {
  502. //Play Input Click Sound.
  503. UIDevice.current.playInputClick()
  504. }
  505. if let textFieldRetain = _textFieldView {
  506. //Resign textFieldView.
  507. let isResignedFirstResponder = resignFirstResponder()
  508. var invocation = barButton.invocation
  509. var sender = textFieldRetain
  510. //Handling search bar special case
  511. do {
  512. if let searchBar = textFieldRetain.textFieldSearchBar() {
  513. invocation = searchBar.keyboardToolbar.doneBarButton.invocation
  514. sender = searchBar
  515. }
  516. }
  517. if isResignedFirstResponder {
  518. invocation?.invoke(from: sender)
  519. }
  520. }
  521. }
  522. /** Resigning on tap gesture. (Enhancement ID: #14)*/
  523. @objc internal func tapRecognized(_ gesture: UITapGestureRecognizer) {
  524. if gesture.state == .ended {
  525. //Resigning currently responder textField.
  526. resignFirstResponder()
  527. }
  528. }
  529. /** 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. */
  530. @objc public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  531. return false
  532. }
  533. /** To not detect touch events in a subclass of UIControl, these may have added their own selector for specific work */
  534. @objc public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  535. // Should not recognize gesture if the clicked view is either UIControl or UINavigationBar(<Back button etc...) (Bug ID: #145)
  536. for ignoreClass in touchResignedGestureIgnoreClasses {
  537. if touch.view?.isKind(of: ignoreClass) == true {
  538. return false
  539. }
  540. }
  541. return true
  542. }
  543. ///-----------------------
  544. /// MARK: UISound handling
  545. ///-----------------------
  546. /**
  547. If YES, then it plays inputClick sound on next/previous/done click.
  548. */
  549. @objc public var shouldPlayInputClicks = true
  550. ///---------------------------
  551. /// MARK: UIAnimation handling
  552. ///---------------------------
  553. /**
  554. If YES, then calls 'setNeedsLayout' and 'layoutIfNeeded' on any frame update of to viewController's view.
  555. */
  556. @objc public var layoutIfNeededOnUpdate = false
  557. ///------------------------------------
  558. /// MARK: Class Level disabling methods
  559. ///------------------------------------
  560. /**
  561. Disable distance handling within the scope of disabled distance handling viewControllers classes. Within this scope, 'enabled' property is ignored. Class should be kind of UIViewController.
  562. */
  563. @objc public var disabledDistanceHandlingClasses = [UIViewController.Type]()
  564. /**
  565. Enable distance handling within the scope of enabled distance handling viewControllers classes. Within this scope, 'enabled' property is ignored. Class should be kind of UIViewController. If same Class is added in disabledDistanceHandlingClasses list, then enabledDistanceHandlingClasses will be ignored.
  566. */
  567. @objc public var enabledDistanceHandlingClasses = [UIViewController.Type]()
  568. /**
  569. Disable automatic toolbar creation within the scope of disabled toolbar viewControllers classes. Within this scope, 'enableAutoToolbar' property is ignored. Class should be kind of UIViewController.
  570. */
  571. @objc public var disabledToolbarClasses = [UIViewController.Type]()
  572. /**
  573. Enable automatic toolbar creation within the scope of enabled toolbar viewControllers classes. Within this scope, 'enableAutoToolbar' property is ignored. Class should be kind of UIViewController. If same Class is added in disabledToolbarClasses list, then enabledToolbarClasses will be ignore.
  574. */
  575. @objc public var enabledToolbarClasses = [UIViewController.Type]()
  576. /**
  577. Allowed subclasses of UIView to add all inner textField, this will allow to navigate between textField contains in different superview. Class should be kind of UIView.
  578. */
  579. @objc public var toolbarPreviousNextAllowedClasses = [UIView.Type]()
  580. /**
  581. Disabled classes to ignore 'shouldResignOnTouchOutside' property, Class should be kind of UIViewController.
  582. */
  583. @objc public var disabledTouchResignedClasses = [UIViewController.Type]()
  584. /**
  585. Enabled classes to forcefully enable 'shouldResignOnTouchOutsite' property. Class should be kind of UIViewController. If same Class is added in disabledTouchResignedClasses list, then enabledTouchResignedClasses will be ignored.
  586. */
  587. @objc public var enabledTouchResignedClasses = [UIViewController.Type]()
  588. /**
  589. if shouldResignOnTouchOutside is enabled then you can customise the behaviour to not recognise gesture touches on some specific view subclasses. Class should be kind of UIView. Default is [UIControl, UINavigationBar]
  590. */
  591. @objc public var touchResignedGestureIgnoreClasses = [UIView.Type]()
  592. ///----------------------------------
  593. /// MARK: Third Party Library support
  594. /// Add TextField/TextView Notifications customised Notifications. For example while using YYTextView https://github.com/ibireme/YYText
  595. ///----------------------------------
  596. /**
  597. Add/Remove customised Notification for third party customised TextField/TextView. Please be aware that the Notification object must be idential to UITextField/UITextView Notification objects and customised TextField/TextView support must be idential to UITextField/UITextView.
  598. @param didBeginEditingNotificationName This should be identical to UITextViewTextDidBeginEditingNotification
  599. @param didEndEditingNotificationName This should be identical to UITextViewTextDidEndEditingNotification
  600. */
  601. @objc public func registerTextFieldViewClass(_ aClass: UIView.Type, didBeginEditingNotificationName: String, didEndEditingNotificationName: String) {
  602. NotificationCenter.default.addObserver(self, selector: #selector(self.textFieldViewDidBeginEditing(_:)), name: Notification.Name(rawValue: didBeginEditingNotificationName), object: nil)
  603. NotificationCenter.default.addObserver(self, selector: #selector(self.textFieldViewDidEndEditing(_:)), name: Notification.Name(rawValue: didEndEditingNotificationName), object: nil)
  604. }
  605. @objc public func unregisterTextFieldViewClass(_ aClass: UIView.Type, didBeginEditingNotificationName: String, didEndEditingNotificationName: String) {
  606. NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: didBeginEditingNotificationName), object: nil)
  607. NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: didEndEditingNotificationName), object: nil)
  608. }
  609. /**************************************************************************************/
  610. ///------------------------
  611. /// MARK: Private variables
  612. ///------------------------
  613. /*******************************************/
  614. /** To save UITextField/UITextView object voa textField/textView notifications. */
  615. private weak var _textFieldView: UIView?
  616. /** To save rootViewController.view.frame.origin. */
  617. private var _topViewBeginOrigin = IQKeyboardManager.kIQCGPointInvalid
  618. /** To overcome with popGestureRecognizer issue Bug ID: #1361 */
  619. private weak var _rootViewControllerWhilePopGestureRecognizerActive: UIViewController?
  620. private var _topViewBeginOriginWhilePopGestureRecognizerActive = IQKeyboardManager.kIQCGPointInvalid
  621. /** To save rootViewController */
  622. private weak var _rootViewController: UIViewController?
  623. /*******************************************/
  624. /** Variable to save lastScrollView that was scrolled. */
  625. private weak var _lastScrollView: UIScrollView?
  626. /** LastScrollView's initial contentOffset. */
  627. private var _startingContentOffset = CGPoint.zero
  628. /** LastScrollView's initial scrollIndicatorInsets. */
  629. private var _startingScrollIndicatorInsets = UIEdgeInsets()
  630. /** LastScrollView's initial contentInsets. */
  631. private var _startingContentInsets = UIEdgeInsets()
  632. /*******************************************/
  633. /** To save keyboardWillShowNotification. Needed for enable keyboard functionality. */
  634. private var _kbShowNotification: Notification?
  635. /** To save keyboard rame. */
  636. private var _kbFrame = CGRect.zero
  637. /** To save keyboard animation duration. */
  638. private var _animationDuration: TimeInterval = 0.25
  639. /** To mimic the keyboard animation */
  640. #if swift(>=4.2)
  641. private var _animationCurve: UIView.AnimationOptions = .curveEaseOut
  642. #else
  643. private var _animationCurve: UIViewAnimationOptions = .curveEaseOut
  644. #endif
  645. /*******************************************/
  646. /** Boolean to maintain keyboard is showing or it is hide. To solve rootViewController.view.frame calculations. */
  647. private var _privateIsKeyboardShowing = false
  648. private var _privateMovedDistance: CGFloat = 0.0 {
  649. didSet {
  650. movedDistanceChanged?(_privateMovedDistance)
  651. }
  652. }
  653. /** To use with keyboardDistanceFromTextField. */
  654. private var _privateKeyboardDistanceFromTextField: CGFloat = 10.0
  655. /** To know if we have any pending request to adjust view position. */
  656. private var _privateHasPendingAdjustRequest = false
  657. /**************************************************************************************/
  658. ///--------------------------------------
  659. /// MARK: Initialization/Deinitialization
  660. ///--------------------------------------
  661. /* Singleton Object Initialization. */
  662. override init() {
  663. super.init()
  664. self.registerAllNotifications()
  665. //Creating gesture for @shouldResignOnTouchOutside. (Enhancement ID: #14)
  666. resignFirstResponderGesture.isEnabled = shouldResignOnTouchOutside
  667. //Loading IQToolbar, IQTitleBarButtonItem, IQBarButtonItem to fix first time keyboard appearance delay (Bug ID: #550)
  668. //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
  669. let textField = UITextField()
  670. textField.addDoneOnKeyboardWithTarget(nil, action: #selector(self.doneAction(_:)))
  671. textField.addPreviousNextDoneOnKeyboardWithTarget(nil, previousAction: #selector(self.previousAction(_:)), nextAction: #selector(self.nextAction(_:)), doneAction: #selector(self.doneAction(_:)))
  672. disabledDistanceHandlingClasses.append(UITableViewController.self)
  673. disabledDistanceHandlingClasses.append(UIAlertController.self)
  674. disabledToolbarClasses.append(UIAlertController.self)
  675. disabledTouchResignedClasses.append(UIAlertController.self)
  676. toolbarPreviousNextAllowedClasses.append(UITableView.self)
  677. toolbarPreviousNextAllowedClasses.append(UICollectionView.self)
  678. toolbarPreviousNextAllowedClasses.append(IQPreviousNextView.self)
  679. touchResignedGestureIgnoreClasses.append(UIControl.self)
  680. touchResignedGestureIgnoreClasses.append(UINavigationBar.self)
  681. }
  682. /** Override +load method to enable KeyboardManager when class loader load IQKeyboardManager. Enabling when app starts (No need to write any code) */
  683. /** It doesn't work from Swift 1.2 */
  684. // override public class func load() {
  685. // super.load()
  686. //
  687. // //Enabling IQKeyboardManager.
  688. // IQKeyboardManager.shared.enable = true
  689. // }
  690. deinit {
  691. // Disable the keyboard manager.
  692. enable = false
  693. //Removing notification observers on dealloc.
  694. NotificationCenter.default.removeObserver(self)
  695. }
  696. /** Getting keyWindow. */
  697. private func keyWindow() -> UIWindow? {
  698. if let keyWindow = _textFieldView?.window {
  699. return keyWindow
  700. } else {
  701. struct Static {
  702. /** @abstract Save keyWindow object for reuse.
  703. @discussion Sometimes [[UIApplication sharedApplication] keyWindow] is returning nil between the app. */
  704. static weak var keyWindow: UIWindow?
  705. }
  706. //If original key window is not nil and the cached keywindow is also not original keywindow then changing keywindow.
  707. if let originalKeyWindow = UIApplication.shared.keyWindow,
  708. (Static.keyWindow == nil || Static.keyWindow != originalKeyWindow) {
  709. Static.keyWindow = originalKeyWindow
  710. }
  711. //Return KeyWindow
  712. return Static.keyWindow
  713. }
  714. }
  715. ///-----------------------
  716. /// MARK: Helper Functions
  717. ///-----------------------
  718. private func optimizedAdjustPosition() {
  719. if _privateHasPendingAdjustRequest == false {
  720. _privateHasPendingAdjustRequest = true
  721. OperationQueue.main.addOperation {
  722. self.adjustPosition()
  723. self._privateHasPendingAdjustRequest = false
  724. }
  725. }
  726. }
  727. /* Adjusting RootViewController's frame according to interface orientation. */
  728. private func adjustPosition() {
  729. // We are unable to get textField object while keyboard showing on WKWebView's textField. (Bug ID: #11)
  730. if _privateHasPendingAdjustRequest == true,
  731. let textFieldView = _textFieldView,
  732. let rootController = textFieldView.parentContainerViewController(),
  733. let window = keyWindow(),
  734. let textFieldViewRectInWindow = textFieldView.superview?.convert(textFieldView.frame, to: window),
  735. let textFieldViewRectInRootSuperview = textFieldView.superview?.convert(textFieldView.frame, to: rootController.view?.superview) {
  736. let startTime = CACurrentMediaTime()
  737. showLog("****** \(#function) started ******", indentation: 1)
  738. // Getting RootViewOrigin.
  739. var rootViewOrigin = rootController.view.frame.origin
  740. //Maintain keyboardDistanceFromTextField
  741. var specialKeyboardDistanceFromTextField = textFieldView.keyboardDistanceFromTextField
  742. if let searchBar = textFieldView.textFieldSearchBar() {
  743. specialKeyboardDistanceFromTextField = searchBar.keyboardDistanceFromTextField
  744. }
  745. let newKeyboardDistanceFromTextField = (specialKeyboardDistanceFromTextField == kIQUseDefaultKeyboardDistance) ? keyboardDistanceFromTextField : specialKeyboardDistanceFromTextField
  746. var kbSize = _kbFrame.size
  747. do {
  748. var kbFrame = _kbFrame
  749. kbFrame.origin.y -= newKeyboardDistanceFromTextField
  750. kbFrame.size.height += newKeyboardDistanceFromTextField
  751. //Calculating actual keyboard covered size respect to window, keyboard frame may be different when hardware keyboard is attached (Bug ID: #469) (Bug ID: #381) (Bug ID: #1506)
  752. let intersectRect = kbFrame.intersection(window.frame)
  753. if intersectRect.isNull {
  754. kbSize = CGSize(width: kbFrame.size.width, height: 0)
  755. } else {
  756. kbSize = intersectRect.size
  757. }
  758. }
  759. let statusBarHeight : CGFloat
  760. #if swift(>=5.1)
  761. if #available(iOS 13, *) {
  762. statusBarHeight = window.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
  763. } else {
  764. statusBarHeight = UIApplication.shared.statusBarFrame.height
  765. }
  766. #else
  767. statusBarHeight = UIApplication.shared.statusBarFrame.height
  768. #endif
  769. let navigationBarAreaHeight: CGFloat = statusBarHeight + ( rootController.navigationController?.navigationBar.frame.height ?? 0)
  770. let layoutAreaHeight: CGFloat = rootController.view.layoutMargins.bottom
  771. let topLayoutGuide: CGFloat = max(navigationBarAreaHeight, layoutAreaHeight) + 5
  772. let bottomLayoutGuide: CGFloat = (textFieldView is UIScrollView && textFieldView.responds(to: #selector(getter: UITextView.isEditable))) ? 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.
  773. // Move positive = textField is hidden.
  774. // Move negative = textField is showing.
  775. // Calculating move position.
  776. var move: CGFloat = min(textFieldViewRectInRootSuperview.minY-(topLayoutGuide), textFieldViewRectInWindow.maxY-(window.frame.height-kbSize.height)+bottomLayoutGuide)
  777. showLog("Need to move: \(move)")
  778. var superScrollView: UIScrollView?
  779. var superView = textFieldView.superviewOfClassType(UIScrollView.self) as? UIScrollView
  780. //Getting UIScrollView whose scrolling is enabled. // (Bug ID: #285)
  781. while let view = superView {
  782. if view.isScrollEnabled && view.shouldIgnoreScrollingAdjustment == false {
  783. superScrollView = view
  784. break
  785. } else {
  786. // Getting it's superScrollView. // (Enhancement ID: #21, #24)
  787. superView = view.superviewOfClassType(UIScrollView.self) as? UIScrollView
  788. }
  789. }
  790. //If there was a lastScrollView. // (Bug ID: #34)
  791. if let lastScrollView = _lastScrollView {
  792. //If we can't find current superScrollView, then setting lastScrollView to it's original form.
  793. if superScrollView == nil {
  794. if lastScrollView.contentInset != self._startingContentInsets {
  795. showLog("Restoring contentInset to: \(_startingContentInsets)")
  796. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  797. lastScrollView.contentInset = self._startingContentInsets
  798. lastScrollView.scrollIndicatorInsets = self._startingScrollIndicatorInsets
  799. })
  800. }
  801. if lastScrollView.shouldRestoreScrollViewContentOffset == true && lastScrollView.contentOffset.equalTo(_startingContentOffset) == false {
  802. showLog("Restoring contentOffset to: \(_startingContentOffset)")
  803. var animatedContentOffset = false // (Bug ID: #1365, #1508, #1541)
  804. if #available(iOS 9, *) {
  805. animatedContentOffset = textFieldView.superviewOfClassType(UIStackView.self, belowView: lastScrollView) != nil
  806. }
  807. if animatedContentOffset {
  808. lastScrollView.setContentOffset(_startingContentOffset, animated: UIView.areAnimationsEnabled)
  809. } else {
  810. lastScrollView.contentOffset = _startingContentOffset
  811. }
  812. }
  813. _startingContentInsets = UIEdgeInsets()
  814. _startingScrollIndicatorInsets = UIEdgeInsets()
  815. _startingContentOffset = CGPoint.zero
  816. _lastScrollView = nil
  817. } else if superScrollView != lastScrollView { //If both scrollView's are different, then reset lastScrollView to it's original frame and setting current scrollView as last scrollView.
  818. if lastScrollView.contentInset != self._startingContentInsets {
  819. showLog("Restoring contentInset to: \(_startingContentInsets)")
  820. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  821. lastScrollView.contentInset = self._startingContentInsets
  822. lastScrollView.scrollIndicatorInsets = self._startingScrollIndicatorInsets
  823. })
  824. }
  825. if lastScrollView.shouldRestoreScrollViewContentOffset == true && lastScrollView.contentOffset.equalTo(_startingContentOffset) == false {
  826. showLog("Restoring contentOffset to: \(_startingContentOffset)")
  827. var animatedContentOffset = false // (Bug ID: #1365, #1508, #1541)
  828. if #available(iOS 9, *) {
  829. animatedContentOffset = textFieldView.superviewOfClassType(UIStackView.self, belowView: lastScrollView) != nil
  830. }
  831. if animatedContentOffset {
  832. lastScrollView.setContentOffset(_startingContentOffset, animated: UIView.areAnimationsEnabled)
  833. } else {
  834. lastScrollView.contentOffset = _startingContentOffset
  835. }
  836. }
  837. _lastScrollView = superScrollView
  838. if let scrollView = superScrollView {
  839. _startingContentInsets = scrollView.contentInset
  840. _startingContentOffset = scrollView.contentOffset
  841. #if swift(>=5.1)
  842. if #available(iOS 11.1, *) {
  843. _startingScrollIndicatorInsets = scrollView.verticalScrollIndicatorInsets
  844. } else {
  845. _startingScrollIndicatorInsets = scrollView.scrollIndicatorInsets
  846. }
  847. #else
  848. _startingScrollIndicatorInsets = scrollView.scrollIndicatorInsets
  849. #endif
  850. }
  851. showLog("Saving ScrollView New contentInset: \(_startingContentInsets) and contentOffset: \(_startingContentOffset)")
  852. }
  853. //Else the case where superScrollView == lastScrollView means we are on same scrollView after switching to different textField. So doing nothing, going ahead
  854. } else if let unwrappedSuperScrollView = superScrollView { //If there was no lastScrollView and we found a current scrollView. then setting it as lastScrollView.
  855. _lastScrollView = unwrappedSuperScrollView
  856. _startingContentInsets = unwrappedSuperScrollView.contentInset
  857. _startingContentOffset = unwrappedSuperScrollView.contentOffset
  858. #if swift(>=5.1)
  859. if #available(iOS 11.1, *) {
  860. _startingScrollIndicatorInsets = unwrappedSuperScrollView.verticalScrollIndicatorInsets
  861. } else {
  862. _startingScrollIndicatorInsets = unwrappedSuperScrollView.scrollIndicatorInsets
  863. }
  864. #else
  865. _startingScrollIndicatorInsets = unwrappedSuperScrollView.scrollIndicatorInsets
  866. #endif
  867. showLog("Saving ScrollView contentInset: \(_startingContentInsets) and contentOffset: \(_startingContentOffset)")
  868. }
  869. // Special case for ScrollView.
  870. // If we found lastScrollView then setting it's contentOffset to show textField.
  871. if let lastScrollView = _lastScrollView {
  872. //Saving
  873. var lastView = textFieldView
  874. var superScrollView = _lastScrollView
  875. while let scrollView = superScrollView {
  876. var shouldContinue = false
  877. if move > 0 {
  878. shouldContinue = move > (-scrollView.contentOffset.y - scrollView.contentInset.top)
  879. } else if let tableView = scrollView.superviewOfClassType(UITableView.self) as? UITableView {
  880. shouldContinue = scrollView.contentOffset.y > 0
  881. if shouldContinue == true, let tableCell = textFieldView.superviewOfClassType(UITableViewCell.self) as? UITableViewCell, let indexPath = tableView.indexPath(for: tableCell), let previousIndexPath = tableView.previousIndexPath(of: indexPath) {
  882. let previousCellRect = tableView.rectForRow(at: previousIndexPath)
  883. if previousCellRect.isEmpty == false {
  884. let previousCellRectInRootSuperview = tableView.convert(previousCellRect, to: rootController.view.superview)
  885. move = min(0, previousCellRectInRootSuperview.maxY - topLayoutGuide)
  886. }
  887. }
  888. } else if let collectionView = scrollView.superviewOfClassType(UICollectionView.self) as? UICollectionView {
  889. shouldContinue = scrollView.contentOffset.y > 0
  890. if shouldContinue == true, let collectionCell = textFieldView.superviewOfClassType(UICollectionViewCell.self) as? UICollectionViewCell, let indexPath = collectionView.indexPath(for: collectionCell), let previousIndexPath = collectionView.previousIndexPath(of: indexPath), let attributes = collectionView.layoutAttributesForItem(at: previousIndexPath) {
  891. let previousCellRect = attributes.frame
  892. if previousCellRect.isEmpty == false {
  893. let previousCellRectInRootSuperview = collectionView.convert(previousCellRect, to: rootController.view.superview)
  894. move = min(0, previousCellRectInRootSuperview.maxY - topLayoutGuide)
  895. }
  896. }
  897. } else {
  898. shouldContinue = textFieldViewRectInRootSuperview.origin.y < topLayoutGuide
  899. if shouldContinue {
  900. move = min(0, textFieldViewRectInRootSuperview.origin.y - topLayoutGuide)
  901. }
  902. }
  903. //Looping in upper hierarchy until we don't found any scrollView in it's upper hirarchy till UIWindow object.
  904. if shouldContinue {
  905. var tempScrollView = scrollView.superviewOfClassType(UIScrollView.self) as? UIScrollView
  906. var nextScrollView: UIScrollView?
  907. while let view = tempScrollView {
  908. if view.isScrollEnabled && view.shouldIgnoreScrollingAdjustment == false {
  909. nextScrollView = view
  910. break
  911. } else {
  912. tempScrollView = view.superviewOfClassType(UIScrollView.self) as? UIScrollView
  913. }
  914. }
  915. //Getting lastViewRect.
  916. if let lastViewRect = lastView.superview?.convert(lastView.frame, to: scrollView) {
  917. //Calculating the expected Y offset from move and scrollView's contentOffset.
  918. var shouldOffsetY = scrollView.contentOffset.y - min(scrollView.contentOffset.y, -move)
  919. //Rearranging the expected Y offset according to the view.
  920. shouldOffsetY = min(shouldOffsetY, lastViewRect.origin.y)
  921. //[_textFieldView isKindOfClass:[UITextView class]] If is a UITextView type
  922. //nextScrollView == nil If processing scrollView is last scrollView in upper hierarchy (there is no other scrollView upper hierrchy.)
  923. //[_textFieldView isKindOfClass:[UITextView class]] If is a UITextView type
  924. //shouldOffsetY >= 0 shouldOffsetY must be greater than in order to keep distance from navigationBar (Bug ID: #92)
  925. if (textFieldView is UIScrollView && textFieldView.responds(to: #selector(getter: UITextView.isEditable))) &&
  926. nextScrollView == nil &&
  927. shouldOffsetY >= 0 {
  928. // Converting Rectangle according to window bounds.
  929. if let currentTextFieldViewRect = textFieldView.superview?.convert(textFieldView.frame, to: window) {
  930. //Calculating expected fix distance which needs to be managed from navigation bar
  931. let expectedFixDistance = currentTextFieldViewRect.minY - topLayoutGuide
  932. //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)
  933. shouldOffsetY = min(shouldOffsetY, scrollView.contentOffset.y + expectedFixDistance)
  934. //Setting move to 0 because now we don't want to move any view anymore (All will be managed by our contentInset logic.
  935. move = 0
  936. } else {
  937. //Subtracting the Y offset from the move variable, because we are going to change scrollView's contentOffset.y to shouldOffsetY.
  938. move -= (shouldOffsetY-scrollView.contentOffset.y)
  939. }
  940. } else {
  941. //Subtracting the Y offset from the move variable, because we are going to change scrollView's contentOffset.y to shouldOffsetY.
  942. move -= (shouldOffsetY-scrollView.contentOffset.y)
  943. }
  944. let newContentOffset = CGPoint(x: scrollView.contentOffset.x, y: shouldOffsetY)
  945. if scrollView.contentOffset.equalTo(newContentOffset) == false {
  946. showLog("old contentOffset: \(scrollView.contentOffset) new contentOffset: \(newContentOffset)")
  947. self.showLog("Remaining Move: \(move)")
  948. //Getting problem while using `setContentOffset:animated:`, So I used animation API.
  949. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  950. var animatedContentOffset = false // (Bug ID: #1365, #1508, #1541)
  951. if #available(iOS 9, *) {
  952. animatedContentOffset = textFieldView.superviewOfClassType(UIStackView.self, belowView: scrollView) != nil
  953. }
  954. if animatedContentOffset {
  955. scrollView.setContentOffset(newContentOffset, animated: UIView.areAnimationsEnabled)
  956. } else {
  957. scrollView.contentOffset = newContentOffset
  958. }
  959. }) { _ in
  960. if scrollView is UITableView || scrollView is UICollectionView {
  961. //This will update the next/previous states
  962. self.addToolbarIfRequired()
  963. }
  964. }
  965. }
  966. }
  967. // Getting next lastView & superScrollView.
  968. lastView = scrollView
  969. superScrollView = nextScrollView
  970. } else {
  971. move = 0
  972. break
  973. }
  974. }
  975. //Updating contentInset
  976. if let lastScrollViewRect = lastScrollView.superview?.convert(lastScrollView.frame, to: window) {
  977. let bottom: CGFloat = (kbSize.height-newKeyboardDistanceFromTextField)-(window.frame.height-lastScrollViewRect.maxY)
  978. // Update the insets so that the scroll vew doesn't shift incorrectly when the offset is near the bottom of the scroll view.
  979. var bottomInset = max(_startingContentInsets.bottom, bottom)
  980. #if swift(>=4.0)
  981. if #available(iOS 11, *) {
  982. bottomInset -= lastScrollView.safeAreaInsets.bottom
  983. }
  984. #endif
  985. var movedInsets = lastScrollView.contentInset
  986. movedInsets.bottom = bottomInset
  987. if lastScrollView.contentInset != movedInsets {
  988. showLog("old ContentInset: \(lastScrollView.contentInset) new ContentInset: \(movedInsets)")
  989. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  990. lastScrollView.contentInset = movedInsets
  991. var newInset : UIEdgeInsets
  992. #if swift(>=5.1)
  993. if #available(iOS 11.1, *) {
  994. newInset = lastScrollView.verticalScrollIndicatorInsets
  995. } else {
  996. newInset = lastScrollView.scrollIndicatorInsets
  997. }
  998. #else
  999. newInset = lastScrollView.scrollIndicatorInsets
  1000. #endif
  1001. newInset.bottom = movedInsets.bottom
  1002. lastScrollView.scrollIndicatorInsets = newInset
  1003. })
  1004. }
  1005. }
  1006. }
  1007. //Going ahead. No else if.
  1008. //Special case for UITextView(Readjusting textView.contentInset when textView hight is too big to fit on screen)
  1009. //_lastScrollView If not having inside any scrollView, (now contentInset manages the full screen textView.
  1010. //[_textFieldView isKindOfClass:[UITextView class]] If is a UITextView type
  1011. if let textView = textFieldView as? UIScrollView, textFieldView.responds(to: #selector(getter: UITextView.isEditable)) {
  1012. // CGRect rootSuperViewFrameInWindow = [_rootViewController.view.superview convertRect:_rootViewController.view.superview.bounds toView:keyWindow];
  1013. //
  1014. // CGFloat keyboardOverlapping = CGRectGetMaxY(rootSuperViewFrameInWindow) - keyboardYPosition;
  1015. //
  1016. // CGFloat textViewHeight = MIN(CGRectGetHeight(_textFieldView.frame), (CGRectGetHeight(rootSuperViewFrameInWindow)-topLayoutGuide-keyboardOverlapping));
  1017. let keyboardYPosition = window.frame.height - (kbSize.height-newKeyboardDistanceFromTextField)
  1018. var rootSuperViewFrameInWindow = window.frame
  1019. if let rootSuperview = rootController.view.superview {
  1020. rootSuperViewFrameInWindow = rootSuperview.convert(rootSuperview.bounds, to: window)
  1021. }
  1022. let keyboardOverlapping = rootSuperViewFrameInWindow.maxY - keyboardYPosition
  1023. let textViewHeight = min(textView.frame.height, rootSuperViewFrameInWindow.height-topLayoutGuide-keyboardOverlapping)
  1024. if textView.frame.size.height-textView.contentInset.bottom>textViewHeight {
  1025. //_isTextViewContentInsetChanged, If frame is not change by library in past, then saving user textView properties (Bug ID: #92)
  1026. if self.isTextViewContentInsetChanged == false {
  1027. self.startingTextViewContentInsets = textView.contentInset
  1028. #if swift(>=5.1)
  1029. if #available(iOS 11.1, *) {
  1030. self.startingTextViewScrollIndicatorInsets = textView.verticalScrollIndicatorInsets
  1031. } else {
  1032. self.startingTextViewScrollIndicatorInsets = textView.scrollIndicatorInsets
  1033. }
  1034. #else
  1035. self.startingTextViewScrollIndicatorInsets = textView.scrollIndicatorInsets
  1036. #endif
  1037. }
  1038. self.isTextViewContentInsetChanged = true
  1039. var newContentInset = textView.contentInset
  1040. newContentInset.bottom = textView.frame.size.height-textViewHeight
  1041. #if swift(>=4.0)
  1042. if #available(iOS 11, *) {
  1043. newContentInset.bottom -= textView.safeAreaInsets.bottom
  1044. }
  1045. #endif
  1046. if textView.contentInset != newContentInset {
  1047. self.showLog("\(textFieldView) Old UITextView.contentInset: \(textView.contentInset) New UITextView.contentInset: \(newContentInset)")
  1048. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1049. textView.contentInset = newContentInset
  1050. textView.scrollIndicatorInsets = newContentInset
  1051. }, completion: { (_) -> Void in })
  1052. }
  1053. }
  1054. }
  1055. // +Positive or zero.
  1056. if move >= 0 {
  1057. rootViewOrigin.y = max(rootViewOrigin.y - move, min(0, -(kbSize.height-newKeyboardDistanceFromTextField)))
  1058. if rootController.view.frame.origin.equalTo(rootViewOrigin) == false {
  1059. showLog("Moving Upward")
  1060. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1061. var rect = rootController.view.frame
  1062. rect.origin = rootViewOrigin
  1063. rootController.view.frame = rect
  1064. //Animating content if needed (Bug ID: #204)
  1065. if self.layoutIfNeededOnUpdate == true {
  1066. //Animating content (Bug ID: #160)
  1067. rootController.view.setNeedsLayout()
  1068. rootController.view.layoutIfNeeded()
  1069. }
  1070. self.showLog("Set \(rootController) origin to: \(rootViewOrigin)")
  1071. })
  1072. }
  1073. _privateMovedDistance = (_topViewBeginOrigin.y-rootViewOrigin.y)
  1074. } else { // -Negative
  1075. let disturbDistance: CGFloat = rootViewOrigin.y-_topViewBeginOrigin.y
  1076. // disturbDistance Negative = frame disturbed.
  1077. // disturbDistance positive = frame not disturbed.
  1078. if disturbDistance <= 0 {
  1079. rootViewOrigin.y -= max(move, disturbDistance)
  1080. if rootController.view.frame.origin.equalTo(rootViewOrigin) == false {
  1081. showLog("Moving Downward")
  1082. // Setting adjusted rootViewRect
  1083. // Setting adjusted rootViewRect
  1084. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1085. var rect = rootController.view.frame
  1086. rect.origin = rootViewOrigin
  1087. rootController.view.frame = rect
  1088. //Animating content if needed (Bug ID: #204)
  1089. if self.layoutIfNeededOnUpdate == true {
  1090. //Animating content (Bug ID: #160)
  1091. rootController.view.setNeedsLayout()
  1092. rootController.view.layoutIfNeeded()
  1093. }
  1094. self.showLog("Set \(rootController) origin to: \(rootViewOrigin)")
  1095. })
  1096. }
  1097. _privateMovedDistance = (_topViewBeginOrigin.y-rootViewOrigin.y)
  1098. }
  1099. }
  1100. let elapsedTime = CACurrentMediaTime() - startTime
  1101. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1102. }
  1103. }
  1104. private func restorePosition() {
  1105. _privateHasPendingAdjustRequest = false
  1106. // Setting rootViewController frame to it's original position. // (Bug ID: #18)
  1107. if _topViewBeginOrigin.equalTo(IQKeyboardManager.kIQCGPointInvalid) == false {
  1108. if let rootViewController = _rootViewController {
  1109. if rootViewController.view.frame.origin.equalTo(self._topViewBeginOrigin) == false {
  1110. //Used UIViewAnimationOptionBeginFromCurrentState to minimize strange animations.
  1111. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1112. self.showLog("Restoring \(rootViewController) origin to: \(self._topViewBeginOrigin)")
  1113. // Setting it's new frame
  1114. var rect = rootViewController.view.frame
  1115. rect.origin = self._topViewBeginOrigin
  1116. rootViewController.view.frame = rect
  1117. //Animating content if needed (Bug ID: #204)
  1118. if self.layoutIfNeededOnUpdate == true {
  1119. //Animating content (Bug ID: #160)
  1120. rootViewController.view.setNeedsLayout()
  1121. rootViewController.view.layoutIfNeeded()
  1122. }
  1123. })
  1124. }
  1125. self._privateMovedDistance = 0
  1126. if rootViewController.navigationController?.interactivePopGestureRecognizer?.state == .began {
  1127. self._rootViewControllerWhilePopGestureRecognizerActive = rootViewController
  1128. self._topViewBeginOriginWhilePopGestureRecognizerActive = self._topViewBeginOrigin
  1129. }
  1130. _rootViewController = nil
  1131. }
  1132. }
  1133. }
  1134. ///---------------------
  1135. /// MARK: Public Methods
  1136. ///---------------------
  1137. /* Refreshes textField/textView position if any external changes is explicitly made by user. */
  1138. @objc public func reloadLayoutIfNeeded() {
  1139. if privateIsEnabled() == true {
  1140. if _privateIsKeyboardShowing == true,
  1141. _topViewBeginOrigin.equalTo(IQKeyboardManager.kIQCGPointInvalid) == false,
  1142. let textFieldView = _textFieldView,
  1143. textFieldView.isAlertViewTextField() == false {
  1144. optimizedAdjustPosition()
  1145. }
  1146. }
  1147. }
  1148. ///-------------------------------
  1149. /// MARK: UIKeyboard Notifications
  1150. ///-------------------------------
  1151. /* UIKeyboardWillShowNotification. */
  1152. @objc internal func keyboardWillShow(_ notification: Notification?) {
  1153. _kbShowNotification = notification
  1154. // Boolean to know keyboard is showing/hiding
  1155. _privateIsKeyboardShowing = true
  1156. let oldKBFrame = _kbFrame
  1157. if let info = notification?.userInfo {
  1158. #if swift(>=4.2)
  1159. let curveUserInfoKey = UIResponder.keyboardAnimationCurveUserInfoKey
  1160. let durationUserInfoKey = UIResponder.keyboardAnimationDurationUserInfoKey
  1161. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  1162. #else
  1163. let curveUserInfoKey = UIKeyboardAnimationCurveUserInfoKey
  1164. let durationUserInfoKey = UIKeyboardAnimationDurationUserInfoKey
  1165. let frameEndUserInfoKey = UIKeyboardFrameEndUserInfoKey
  1166. #endif
  1167. // Getting keyboard animation.
  1168. if let curve = info[curveUserInfoKey] as? UInt {
  1169. _animationCurve = .init(rawValue: curve)
  1170. } else {
  1171. _animationCurve = .curveEaseOut
  1172. }
  1173. // Getting keyboard animation duration
  1174. if let duration = info[durationUserInfoKey] as? TimeInterval {
  1175. //Saving animation duration
  1176. if duration != 0.0 {
  1177. _animationDuration = duration
  1178. }
  1179. } else {
  1180. _animationDuration = 0.25
  1181. }
  1182. // Getting UIKeyboardSize.
  1183. if let kbFrame = info[frameEndUserInfoKey] as? CGRect {
  1184. _kbFrame = kbFrame
  1185. showLog("UIKeyboard Frame: \(_kbFrame)")
  1186. }
  1187. }
  1188. if privateIsEnabled() == false {
  1189. restorePosition()
  1190. _topViewBeginOrigin = IQKeyboardManager.kIQCGPointInvalid
  1191. return
  1192. }
  1193. let startTime = CACurrentMediaTime()
  1194. showLog("****** \(#function) started ******", indentation: 1)
  1195. // (Bug ID: #5)
  1196. if let textFieldView = _textFieldView, _topViewBeginOrigin.equalTo(IQKeyboardManager.kIQCGPointInvalid) == true {
  1197. // keyboard is not showing(At the beginning only). We should save rootViewRect.
  1198. _rootViewController = textFieldView.parentContainerViewController()
  1199. if let controller = _rootViewController {
  1200. if _rootViewControllerWhilePopGestureRecognizerActive == controller {
  1201. _topViewBeginOrigin = _topViewBeginOriginWhilePopGestureRecognizerActive
  1202. } else {
  1203. _topViewBeginOrigin = controller.view.frame.origin
  1204. }
  1205. _rootViewControllerWhilePopGestureRecognizerActive = nil
  1206. _topViewBeginOriginWhilePopGestureRecognizerActive = IQKeyboardManager.kIQCGPointInvalid
  1207. self.showLog("Saving \(controller) beginning origin: \(self._topViewBeginOrigin)")
  1208. }
  1209. }
  1210. //If last restored keyboard size is different(any orientation accure), then refresh. otherwise not.
  1211. if _kbFrame.equalTo(oldKBFrame) == false {
  1212. //If _textFieldView is inside UITableViewController then let UITableViewController to handle it (Bug ID: #37) (Bug ID: #76) See note:- https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html If it is UIAlertView textField then do not affect anything (Bug ID: #70).
  1213. if _privateIsKeyboardShowing == true,
  1214. let textFieldView = _textFieldView,
  1215. textFieldView.isAlertViewTextField() == false {
  1216. // keyboard is already showing. adjust position.
  1217. optimizedAdjustPosition()
  1218. }
  1219. }
  1220. let elapsedTime = CACurrentMediaTime() - startTime
  1221. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1222. }
  1223. /* UIKeyboardDidShowNotification. */
  1224. @objc internal func keyboardDidShow(_ notification: Notification?) {
  1225. if privateIsEnabled() == false {
  1226. return
  1227. }
  1228. let startTime = CACurrentMediaTime()
  1229. showLog("****** \(#function) started ******", indentation: 1)
  1230. if let textFieldView = _textFieldView,
  1231. let parentController = textFieldView.parentContainerViewController(), (parentController.modalPresentationStyle == UIModalPresentationStyle.formSheet || parentController.modalPresentationStyle == UIModalPresentationStyle.pageSheet),
  1232. textFieldView.isAlertViewTextField() == false {
  1233. self.optimizedAdjustPosition()
  1234. }
  1235. let elapsedTime = CACurrentMediaTime() - startTime
  1236. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1237. }
  1238. /* UIKeyboardWillHideNotification. So setting rootViewController to it's default frame. */
  1239. @objc internal func keyboardWillHide(_ notification: Notification?) {
  1240. //If it's not a fake notification generated by [self setEnable:NO].
  1241. if notification != nil {
  1242. _kbShowNotification = nil
  1243. }
  1244. // Boolean to know keyboard is showing/hiding
  1245. _privateIsKeyboardShowing = false
  1246. if let info = notification?.userInfo {
  1247. #if swift(>=4.2)
  1248. let durationUserInfoKey = UIResponder.keyboardAnimationDurationUserInfoKey
  1249. #else
  1250. let durationUserInfoKey = UIKeyboardAnimationDurationUserInfoKey
  1251. #endif
  1252. // Getting keyboard animation duration
  1253. if let duration = info[durationUserInfoKey] as? TimeInterval {
  1254. if duration != 0 {
  1255. // Setitng keyboard animation duration
  1256. _animationDuration = duration
  1257. }
  1258. }
  1259. }
  1260. //If not enabled then do nothing.
  1261. if privateIsEnabled() == false {
  1262. return
  1263. }
  1264. let startTime = CACurrentMediaTime()
  1265. showLog("****** \(#function) started ******", indentation: 1)
  1266. //Commented due to #56. Added all the conditions below to handle WKWebView's textFields. (Bug ID: #56)
  1267. // We are unable to get textField object while keyboard showing on WKWebView's textField. (Bug ID: #11)
  1268. // if (_textFieldView == nil) return
  1269. //Restoring the contentOffset of the lastScrollView
  1270. if let lastScrollView = _lastScrollView {
  1271. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1272. if lastScrollView.contentInset != self._startingContentInsets {
  1273. self.showLog("Restoring contentInset to: \(self._startingContentInsets)")
  1274. lastScrollView.contentInset = self._startingContentInsets
  1275. lastScrollView.scrollIndicatorInsets = self._startingScrollIndicatorInsets
  1276. }
  1277. if lastScrollView.shouldRestoreScrollViewContentOffset == true && lastScrollView.contentOffset.equalTo(self._startingContentOffset) == false {
  1278. self.showLog("Restoring contentOffset to: \(self._startingContentOffset)")
  1279. var animatedContentOffset = false // (Bug ID: #1365, #1508, #1541)
  1280. if #available(iOS 9, *) {
  1281. animatedContentOffset = self._textFieldView?.superviewOfClassType(UIStackView.self, belowView: lastScrollView) != nil
  1282. }
  1283. if animatedContentOffset {
  1284. lastScrollView.setContentOffset(self._startingContentOffset, animated: UIView.areAnimationsEnabled)
  1285. } else {
  1286. lastScrollView.contentOffset = self._startingContentOffset
  1287. }
  1288. }
  1289. // TODO: restore scrollView state
  1290. // This is temporary solution. Have to implement the save and restore scrollView state
  1291. var superScrollView: UIScrollView? = lastScrollView
  1292. while let scrollView = superScrollView {
  1293. let contentSize = CGSize(width: max(scrollView.contentSize.width, scrollView.frame.width), height: max(scrollView.contentSize.height, scrollView.frame.height))
  1294. let minimumY = contentSize.height - scrollView.frame.height
  1295. if minimumY < scrollView.contentOffset.y {
  1296. let newContentOffset = CGPoint(x: scrollView.contentOffset.x, y: minimumY)
  1297. if scrollView.contentOffset.equalTo(newContentOffset) == false {
  1298. var animatedContentOffset = false // (Bug ID: #1365, #1508, #1541)
  1299. if #available(iOS 9, *) {
  1300. animatedContentOffset = self._textFieldView?.superviewOfClassType(UIStackView.self, belowView: scrollView) != nil
  1301. }
  1302. if animatedContentOffset {
  1303. scrollView.setContentOffset(newContentOffset, animated: UIView.areAnimationsEnabled)
  1304. } else {
  1305. scrollView.contentOffset = newContentOffset
  1306. }
  1307. self.showLog("Restoring contentOffset to: \(self._startingContentOffset)")
  1308. }
  1309. }
  1310. superScrollView = scrollView.superviewOfClassType(UIScrollView.self) as? UIScrollView
  1311. }
  1312. })
  1313. }
  1314. restorePosition()
  1315. //Reset all values
  1316. _lastScrollView = nil
  1317. _kbFrame = CGRect.zero
  1318. _startingContentInsets = UIEdgeInsets()
  1319. _startingScrollIndicatorInsets = UIEdgeInsets()
  1320. _startingContentOffset = CGPoint.zero
  1321. // topViewBeginRect = CGRectZero //Commented due to #82
  1322. let elapsedTime = CACurrentMediaTime() - startTime
  1323. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1324. }
  1325. @objc internal func keyboardDidHide(_ notification: Notification) {
  1326. let startTime = CACurrentMediaTime()
  1327. showLog("****** \(#function) started ******", indentation: 1)
  1328. _topViewBeginOrigin = IQKeyboardManager.kIQCGPointInvalid
  1329. _kbFrame = CGRect.zero
  1330. let elapsedTime = CACurrentMediaTime() - startTime
  1331. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1332. }
  1333. ///-------------------------------------------
  1334. /// MARK: UITextField/UITextView Notifications
  1335. ///-------------------------------------------
  1336. /** UITextFieldTextDidBeginEditingNotification, UITextViewTextDidBeginEditingNotification. Fetching UITextFieldView object. */
  1337. @objc internal func textFieldViewDidBeginEditing(_ notification: Notification) {
  1338. let startTime = CACurrentMediaTime()
  1339. showLog("****** \(#function) started ******", indentation: 1)
  1340. // Getting object
  1341. _textFieldView = notification.object as? UIView
  1342. if overrideKeyboardAppearance == true {
  1343. if let textInput = _textFieldView as? UITextInput {
  1344. if textInput.keyboardAppearance != keyboardAppearance {
  1345. //Setting textField keyboard appearance and reloading inputViews.
  1346. if let textFieldView = _textFieldView as? UITextField {
  1347. textFieldView.keyboardAppearance = keyboardAppearance
  1348. } else if let textFieldView = _textFieldView as? UITextView {
  1349. textFieldView.keyboardAppearance = keyboardAppearance
  1350. }
  1351. _textFieldView?.reloadInputViews()
  1352. }
  1353. }
  1354. }
  1355. //If autoToolbar enable, then add toolbar on all the UITextField/UITextView's if required.
  1356. if privateIsEnableAutoToolbar() == true {
  1357. //UITextView special case. Keyboard Notification is firing before textView notification so we need to resign it first and then again set it as first responder to add toolbar on it.
  1358. if let textView = _textFieldView as? UIScrollView, textView.responds(to: #selector(getter: UITextView.isEditable)),
  1359. textView.inputAccessoryView == nil {
  1360. UIView.animate(withDuration: 0.00001, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1361. self.addToolbarIfRequired()
  1362. }, completion: { (_) -> Void in
  1363. //On textView toolbar didn't appear on first time, so forcing textView to reload it's inputViews.
  1364. textView.reloadInputViews()
  1365. })
  1366. } else {
  1367. //Adding toolbar
  1368. addToolbarIfRequired()
  1369. }
  1370. } else {
  1371. removeToolbarIfRequired()
  1372. }
  1373. resignFirstResponderGesture.isEnabled = privateShouldResignOnTouchOutside()
  1374. _textFieldView?.window?.addGestureRecognizer(resignFirstResponderGesture) // (Enhancement ID: #14)
  1375. if privateIsEnabled() == false {
  1376. restorePosition()
  1377. _topViewBeginOrigin = IQKeyboardManager.kIQCGPointInvalid
  1378. } else {
  1379. if _topViewBeginOrigin.equalTo(IQKeyboardManager.kIQCGPointInvalid) == true { // (Bug ID: #5)
  1380. _rootViewController = _textFieldView?.parentContainerViewController()
  1381. if let controller = _rootViewController {
  1382. if _rootViewControllerWhilePopGestureRecognizerActive == controller {
  1383. _topViewBeginOrigin = _topViewBeginOriginWhilePopGestureRecognizerActive
  1384. } else {
  1385. _topViewBeginOrigin = controller.view.frame.origin
  1386. }
  1387. _rootViewControllerWhilePopGestureRecognizerActive = nil
  1388. _topViewBeginOriginWhilePopGestureRecognizerActive = IQKeyboardManager.kIQCGPointInvalid
  1389. self.showLog("Saving \(controller) beginning origin: \(self._topViewBeginOrigin)")
  1390. }
  1391. }
  1392. //If _textFieldView is inside ignored responder then do nothing. (Bug ID: #37, #74, #76)
  1393. //See notes:- https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html If it is UIAlertView textField then do not affect anything (Bug ID: #70).
  1394. if _privateIsKeyboardShowing == true,
  1395. let textFieldView = _textFieldView,
  1396. textFieldView.isAlertViewTextField() == false {
  1397. // keyboard is already showing. adjust position.
  1398. optimizedAdjustPosition()
  1399. }
  1400. }
  1401. let elapsedTime = CACurrentMediaTime() - startTime
  1402. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1403. }
  1404. /** UITextFieldTextDidEndEditingNotification, UITextViewTextDidEndEditingNotification. Removing fetched object. */
  1405. @objc internal func textFieldViewDidEndEditing(_ notification: Notification) {
  1406. let startTime = CACurrentMediaTime()
  1407. showLog("****** \(#function) started ******", indentation: 1)
  1408. //Removing gesture recognizer (Enhancement ID: #14)
  1409. _textFieldView?.window?.removeGestureRecognizer(resignFirstResponderGesture)
  1410. // We check if there's a change in original frame or not.
  1411. if let textView = _textFieldView as? UIScrollView, textView.responds(to: #selector(getter: UITextView.isEditable)) {
  1412. if isTextViewContentInsetChanged == true {
  1413. self.isTextViewContentInsetChanged = false
  1414. if textView.contentInset != self.startingTextViewContentInsets {
  1415. self.showLog("Restoring textView.contentInset to: \(self.startingTextViewContentInsets)")
  1416. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1417. //Setting textField to it's initial contentInset
  1418. textView.contentInset = self.startingTextViewContentInsets
  1419. textView.scrollIndicatorInsets = self.startingTextViewScrollIndicatorInsets
  1420. }, completion: { (_) -> Void in })
  1421. }
  1422. }
  1423. }
  1424. //Setting object to nil
  1425. _textFieldView = nil
  1426. let elapsedTime = CACurrentMediaTime() - startTime
  1427. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1428. }
  1429. ///---------------------------------------
  1430. /// MARK: UIStatusBar Notification methods
  1431. ///---------------------------------------
  1432. /** UIApplicationWillChangeStatusBarOrientationNotification. Need to set the textView to it's original position. If any frame changes made. (Bug ID: #92)*/
  1433. @objc internal func willChangeStatusBarOrientation(_ notification: Notification) {
  1434. let currentStatusBarOrientation : UIInterfaceOrientation
  1435. #if swift(>=5.1)
  1436. if #available(iOS 13, *) {
  1437. currentStatusBarOrientation = keyWindow()?.windowScene?.interfaceOrientation ?? UIInterfaceOrientation.unknown
  1438. } else {
  1439. currentStatusBarOrientation = UIApplication.shared.statusBarOrientation
  1440. }
  1441. #else
  1442. currentStatusBarOrientation = UIApplication.shared.statusBarOrientation
  1443. #endif
  1444. #if swift(>=4.2)
  1445. let statusBarUserInfoKey = UIApplication.statusBarOrientationUserInfoKey
  1446. #else
  1447. let statusBarUserInfoKey = UIApplicationStatusBarOrientationUserInfoKey
  1448. #endif
  1449. guard let statusBarOrientation = notification.userInfo?[statusBarUserInfoKey] as? Int, currentStatusBarOrientation.rawValue != statusBarOrientation else {
  1450. return
  1451. }
  1452. let startTime = CACurrentMediaTime()
  1453. showLog("****** \(#function) started ******", indentation: 1)
  1454. //If textViewContentInsetChanged is saved then restore it.
  1455. if let textView = _textFieldView as? UITextView, textView.responds(to: #selector(getter: UITextView.isEditable)) {
  1456. if isTextViewContentInsetChanged == true {
  1457. self.isTextViewContentInsetChanged = false
  1458. if textView.contentInset != self.startingTextViewContentInsets {
  1459. UIView.animate(withDuration: _animationDuration, delay: 0, options: _animationCurve.union(.beginFromCurrentState), animations: { () -> Void in
  1460. self.showLog("Restoring textView.contentInset to: \(self.startingTextViewContentInsets)")
  1461. //Setting textField to it's initial contentInset
  1462. textView.contentInset = self.startingTextViewContentInsets
  1463. textView.scrollIndicatorInsets = self.startingTextViewScrollIndicatorInsets
  1464. }, completion: { (_) -> Void in })
  1465. }
  1466. }
  1467. }
  1468. restorePosition()
  1469. let elapsedTime = CACurrentMediaTime() - startTime
  1470. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1471. }
  1472. ///------------------
  1473. /// MARK: AutoToolbar
  1474. ///------------------
  1475. /** Get all UITextField/UITextView siblings of textFieldView. */
  1476. private func responderViews() -> [UIView]? {
  1477. var superConsideredView: UIView?
  1478. //If find any consider responderView in it's upper hierarchy then will get deepResponderView.
  1479. for disabledClass in toolbarPreviousNextAllowedClasses {
  1480. superConsideredView = _textFieldView?.superviewOfClassType(disabledClass)
  1481. if superConsideredView != nil {
  1482. break
  1483. }
  1484. }
  1485. //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)
  1486. if let view = superConsideredView {
  1487. return view.deepResponderViews()
  1488. } else { //Otherwise fetching all the siblings
  1489. if let textFields = _textFieldView?.responderSiblings() {
  1490. //Sorting textFields according to behaviour
  1491. switch toolbarManageBehaviour {
  1492. //If autoToolbar behaviour is bySubviews, then returning it.
  1493. case IQAutoToolbarManageBehaviour.bySubviews: return textFields
  1494. //If autoToolbar behaviour is by tag, then sorting it according to tag property.
  1495. case IQAutoToolbarManageBehaviour.byTag: return textFields.sortedArrayByTag()
  1496. //If autoToolbar behaviour is by tag, then sorting it according to tag property.
  1497. case IQAutoToolbarManageBehaviour.byPosition: return textFields.sortedArrayByPosition()
  1498. }
  1499. } else {
  1500. return nil
  1501. }
  1502. }
  1503. }
  1504. /** Add toolbar if it is required to add on textFields and it's siblings. */
  1505. private func addToolbarIfRequired() {
  1506. let startTime = CACurrentMediaTime()
  1507. showLog("****** \(#function) started ******", indentation: 1)
  1508. // Getting all the sibling textFields.
  1509. if let siblings = responderViews(), !siblings.isEmpty {
  1510. showLog("Found \(siblings.count) responder sibling(s)")
  1511. if let textField = _textFieldView {
  1512. //Either there is no inputAccessoryView or if accessoryView is not appropriate for current situation(There is Previous/Next/Done toolbar).
  1513. //setInputAccessoryView: check (Bug ID: #307)
  1514. if textField.responds(to: #selector(setter: UITextField.inputAccessoryView)) {
  1515. if textField.inputAccessoryView == nil ||
  1516. textField.inputAccessoryView?.tag == IQKeyboardManager.kIQPreviousNextButtonToolbarTag ||
  1517. textField.inputAccessoryView?.tag == IQKeyboardManager.kIQDoneButtonToolbarTag {
  1518. let rightConfiguration: IQBarButtonItemConfiguration
  1519. if let doneBarButtonItemImage = toolbarDoneBarButtonItemImage {
  1520. rightConfiguration = IQBarButtonItemConfiguration(image: doneBarButtonItemImage, action: #selector(self.doneAction(_:)))
  1521. } else if let doneBarButtonItemText = toolbarDoneBarButtonItemText {
  1522. rightConfiguration = IQBarButtonItemConfiguration(title: doneBarButtonItemText, action: #selector(self.doneAction(_:)))
  1523. } else {
  1524. rightConfiguration = IQBarButtonItemConfiguration(barButtonSystemItem: .done, action: #selector(self.doneAction(_:)))
  1525. }
  1526. rightConfiguration.accessibilityLabel = toolbarDoneBarButtonItemAccessibilityLabel ?? "Done"
  1527. // If only one object is found, then adding only Done button.
  1528. if (siblings.count <= 1 && previousNextDisplayMode == .default) || previousNextDisplayMode == .alwaysHide {
  1529. textField.addKeyboardToolbarWithTarget(target: self, titleText: (shouldShowToolbarPlaceholder ? textField.drawingToolbarPlaceholder: nil), rightBarButtonConfiguration: rightConfiguration, previousBarButtonConfiguration: nil, nextBarButtonConfiguration: nil)
  1530. textField.inputAccessoryView?.tag = IQKeyboardManager.kIQDoneButtonToolbarTag // (Bug ID: #78)
  1531. } else if previousNextDisplayMode == .default || previousNextDisplayMode == .alwaysShow {
  1532. let prevConfiguration: IQBarButtonItemConfiguration
  1533. if let doneBarButtonItemImage = toolbarPreviousBarButtonItemImage {
  1534. prevConfiguration = IQBarButtonItemConfiguration(image: doneBarButtonItemImage, action: #selector(self.previousAction(_:)))
  1535. } else if let doneBarButtonItemText = toolbarPreviousBarButtonItemText {
  1536. prevConfiguration = IQBarButtonItemConfiguration(title: doneBarButtonItemText, action: #selector(self.previousAction(_:)))
  1537. } else {
  1538. prevConfiguration = IQBarButtonItemConfiguration(image: (UIImage.keyboardPreviousImage() ?? UIImage()), action: #selector(self.previousAction(_:)))
  1539. }
  1540. prevConfiguration.accessibilityLabel = toolbarPreviousBarButtonItemAccessibilityLabel ?? "Previous"
  1541. let nextConfiguration: IQBarButtonItemConfiguration
  1542. if let doneBarButtonItemImage = toolbarNextBarButtonItemImage {
  1543. nextConfiguration = IQBarButtonItemConfiguration(image: doneBarButtonItemImage, action: #selector(self.nextAction(_:)))
  1544. } else if let doneBarButtonItemText = toolbarNextBarButtonItemText {
  1545. nextConfiguration = IQBarButtonItemConfiguration(title: doneBarButtonItemText, action: #selector(self.nextAction(_:)))
  1546. } else {
  1547. nextConfiguration = IQBarButtonItemConfiguration(image: (UIImage.keyboardNextImage() ?? UIImage()), action: #selector(self.nextAction(_:)))
  1548. }
  1549. nextConfiguration.accessibilityLabel = toolbarNextBarButtonItemAccessibilityLabel ?? "Next"
  1550. textField.addKeyboardToolbarWithTarget(target: self, titleText: (shouldShowToolbarPlaceholder ? textField.drawingToolbarPlaceholder: nil), rightBarButtonConfiguration: rightConfiguration, previousBarButtonConfiguration: prevConfiguration, nextBarButtonConfiguration: nextConfiguration)
  1551. textField.inputAccessoryView?.tag = IQKeyboardManager.kIQPreviousNextButtonToolbarTag // (Bug ID: #78)
  1552. }
  1553. let toolbar = textField.keyboardToolbar
  1554. //Setting toolbar tintColor // (Enhancement ID: #30)
  1555. if shouldToolbarUsesTextFieldTintColor {
  1556. toolbar.tintColor = textField.tintColor
  1557. } else if let tintColor = toolbarTintColor {
  1558. toolbar.tintColor = tintColor
  1559. } else {
  1560. toolbar.tintColor = nil
  1561. }
  1562. // Setting toolbar to keyboard.
  1563. if let textFieldView = textField as? UITextInput {
  1564. //Bar style according to keyboard appearance
  1565. switch textFieldView.keyboardAppearance {
  1566. case .dark?:
  1567. toolbar.barStyle = .black
  1568. toolbar.barTintColor = nil
  1569. default:
  1570. toolbar.barStyle = .default
  1571. toolbar.barTintColor = toolbarBarTintColor
  1572. }
  1573. }
  1574. //Setting toolbar title font. // (Enhancement ID: #30)
  1575. if shouldShowToolbarPlaceholder == true &&
  1576. textField.shouldHideToolbarPlaceholder == false {
  1577. //Updating placeholder font to toolbar. //(Bug ID: #148, #272)
  1578. if toolbar.titleBarButton.title == nil ||
  1579. toolbar.titleBarButton.title != textField.drawingToolbarPlaceholder {
  1580. toolbar.titleBarButton.title = textField.drawingToolbarPlaceholder
  1581. }
  1582. //Setting toolbar title font. // (Enhancement ID: #30)
  1583. if let font = placeholderFont {
  1584. toolbar.titleBarButton.titleFont = font
  1585. }
  1586. //Setting toolbar title color. // (Enhancement ID: #880)
  1587. if let color = placeholderColor {
  1588. toolbar.titleBarButton.titleColor = color
  1589. }
  1590. //Setting toolbar button title color. // (Enhancement ID: #880)
  1591. if let color = placeholderButtonColor {
  1592. toolbar.titleBarButton.selectableTitleColor = color
  1593. }
  1594. } else {
  1595. toolbar.titleBarButton.title = nil
  1596. }
  1597. //In case of UITableView (Special), the next/previous buttons has to be refreshed everytime. (Bug ID: #56)
  1598. // If firstTextField, then previous should not be enabled.
  1599. if siblings.first == textField {
  1600. if siblings.count == 1 {
  1601. textField.keyboardToolbar.previousBarButton.isEnabled = false
  1602. textField.keyboardToolbar.nextBarButton.isEnabled = false
  1603. } else {
  1604. textField.keyboardToolbar.previousBarButton.isEnabled = false
  1605. textField.keyboardToolbar.nextBarButton.isEnabled = true
  1606. }
  1607. } else if siblings.last == textField { // If lastTextField then next should not be enaled.
  1608. textField.keyboardToolbar.previousBarButton.isEnabled = true
  1609. textField.keyboardToolbar.nextBarButton.isEnabled = false
  1610. } else {
  1611. textField.keyboardToolbar.previousBarButton.isEnabled = true
  1612. textField.keyboardToolbar.nextBarButton.isEnabled = true
  1613. }
  1614. }
  1615. }
  1616. }
  1617. }
  1618. let elapsedTime = CACurrentMediaTime() - startTime
  1619. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1620. }
  1621. /** Remove any toolbar if it is IQToolbar. */
  1622. private func removeToolbarIfRequired() { // (Bug ID: #18)
  1623. let startTime = CACurrentMediaTime()
  1624. showLog("****** \(#function) started ******", indentation: 1)
  1625. // Getting all the sibling textFields.
  1626. if let siblings = responderViews() {
  1627. showLog("Found \(siblings.count) responder sibling(s)")
  1628. for view in siblings {
  1629. if let toolbar = view.inputAccessoryView as? IQToolbar {
  1630. //setInputAccessoryView: check (Bug ID: #307)
  1631. if view.responds(to: #selector(setter: UITextField.inputAccessoryView)) &&
  1632. (toolbar.tag == IQKeyboardManager.kIQDoneButtonToolbarTag || toolbar.tag == IQKeyboardManager.kIQPreviousNextButtonToolbarTag) {
  1633. if let textField = view as? UITextField {
  1634. textField.inputAccessoryView = nil
  1635. } else if let textView = view as? UITextView {
  1636. textView.inputAccessoryView = nil
  1637. }
  1638. view.reloadInputViews()
  1639. }
  1640. }
  1641. }
  1642. }
  1643. let elapsedTime = CACurrentMediaTime() - startTime
  1644. showLog("****** \(#function) ended: \(elapsedTime) seconds ******", indentation: -1)
  1645. }
  1646. /** reloadInputViews to reload toolbar buttons enable/disable state on the fly Enhancement ID #434. */
  1647. @objc public func reloadInputViews() {
  1648. //If enabled then adding toolbar.
  1649. if privateIsEnableAutoToolbar() == true {
  1650. self.addToolbarIfRequired()
  1651. } else {
  1652. self.removeToolbarIfRequired()
  1653. }
  1654. }
  1655. ///------------------------------------
  1656. /// MARK: Debugging & Developer options
  1657. ///------------------------------------
  1658. @objc public var enableDebugging = false
  1659. /**
  1660. @warning Use below methods to completely enable/disable notifications registered by library internally. Please keep in mind that library is totally dependent on NSNotification of UITextField, UITextField, Keyboard etc. If you do unregisterAllNotifications then library will not work at all. You should only use below methods if you want to completedly disable all library functions. You should use below methods at your own risk.
  1661. */
  1662. @objc public func registerAllNotifications() {
  1663. #if swift(>=4.2)
  1664. let UIKeyboardWillShow = UIResponder.keyboardWillShowNotification
  1665. let UIKeyboardDidShow = UIResponder.keyboardDidShowNotification
  1666. let UIKeyboardWillHide = UIResponder.keyboardWillHideNotification
  1667. let UIKeyboardDidHide = UIResponder.keyboardDidHideNotification
  1668. let UITextFieldTextDidBeginEditing = UITextField.textDidBeginEditingNotification
  1669. let UITextFieldTextDidEndEditing = UITextField.textDidEndEditingNotification
  1670. let UITextViewTextDidBeginEditing = UITextView.textDidBeginEditingNotification
  1671. let UITextViewTextDidEndEditing = UITextView.textDidEndEditingNotification
  1672. let UIApplicationWillChangeStatusBarOrientation = UIApplication.willChangeStatusBarOrientationNotification
  1673. #else
  1674. let UIKeyboardWillShow = Notification.Name.UIKeyboardWillShow
  1675. let UIKeyboardDidShow = Notification.Name.UIKeyboardDidShow
  1676. let UIKeyboardWillHide = Notification.Name.UIKeyboardWillHide
  1677. let UIKeyboardDidHide = Notification.Name.UIKeyboardDidHide
  1678. let UITextFieldTextDidBeginEditing = Notification.Name.UITextFieldTextDidBeginEditing
  1679. let UITextFieldTextDidEndEditing = Notification.Name.UITextFieldTextDidEndEditing
  1680. let UITextViewTextDidBeginEditing = Notification.Name.UITextViewTextDidBeginEditing
  1681. let UITextViewTextDidEndEditing = Notification.Name.UITextViewTextDidEndEditing
  1682. let UIApplicationWillChangeStatusBarOrientation = Notification.Name.UIApplicationWillChangeStatusBarOrientation
  1683. #endif
  1684. // Registering for keyboard notification.
  1685. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIKeyboardWillShow, object: nil)
  1686. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(_:)), name: UIKeyboardDidShow, object: nil)
  1687. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: UIKeyboardWillHide, object: nil)
  1688. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidHide(_:)), name: UIKeyboardDidHide, object: nil)
  1689. // Registering for UITextField notification.
  1690. registerTextFieldViewClass(UITextField.self, didBeginEditingNotificationName: UITextFieldTextDidBeginEditing.rawValue, didEndEditingNotificationName: UITextFieldTextDidEndEditing.rawValue)
  1691. // Registering for UITextView notification.
  1692. registerTextFieldViewClass(UITextView.self, didBeginEditingNotificationName: UITextViewTextDidBeginEditing.rawValue, didEndEditingNotificationName: UITextViewTextDidEndEditing.rawValue)
  1693. // Registering for orientation changes notification
  1694. NotificationCenter.default.addObserver(self, selector: #selector(self.willChangeStatusBarOrientation(_:)), name: UIApplicationWillChangeStatusBarOrientation, object: UIApplication.shared)
  1695. }
  1696. @objc public func unregisterAllNotifications() {
  1697. #if swift(>=4.2)
  1698. let UIKeyboardWillShow = UIResponder.keyboardWillShowNotification
  1699. let UIKeyboardDidShow = UIResponder.keyboardDidShowNotification
  1700. let UIKeyboardWillHide = UIResponder.keyboardWillHideNotification
  1701. let UIKeyboardDidHide = UIResponder.keyboardDidHideNotification
  1702. let UITextFieldTextDidBeginEditing = UITextField.textDidBeginEditingNotification
  1703. let UITextFieldTextDidEndEditing = UITextField.textDidEndEditingNotification
  1704. let UITextViewTextDidBeginEditing = UITextView.textDidBeginEditingNotification
  1705. let UITextViewTextDidEndEditing = UITextView.textDidEndEditingNotification
  1706. let UIApplicationWillChangeStatusBarOrientation = UIApplication.willChangeStatusBarOrientationNotification
  1707. #else
  1708. let UIKeyboardWillShow = Notification.Name.UIKeyboardWillShow
  1709. let UIKeyboardDidShow = Notification.Name.UIKeyboardDidShow
  1710. let UIKeyboardWillHide = Notification.Name.UIKeyboardWillHide
  1711. let UIKeyboardDidHide = Notification.Name.UIKeyboardDidHide
  1712. let UITextFieldTextDidBeginEditing = Notification.Name.UITextFieldTextDidBeginEditing
  1713. let UITextFieldTextDidEndEditing = Notification.Name.UITextFieldTextDidEndEditing
  1714. let UITextViewTextDidBeginEditing = Notification.Name.UITextViewTextDidBeginEditing
  1715. let UITextViewTextDidEndEditing = Notification.Name.UITextViewTextDidEndEditing
  1716. let UIApplicationWillChangeStatusBarOrientation = Notification.Name.UIApplicationWillChangeStatusBarOrientation
  1717. #endif
  1718. // Unregistering for keyboard notification.
  1719. NotificationCenter.default.removeObserver(self, name: UIKeyboardWillShow, object: nil)
  1720. NotificationCenter.default.removeObserver(self, name: UIKeyboardDidShow, object: nil)
  1721. NotificationCenter.default.removeObserver(self, name: UIKeyboardWillHide, object: nil)
  1722. NotificationCenter.default.removeObserver(self, name: UIKeyboardDidHide, object: nil)
  1723. // Unregistering for UITextField notification.
  1724. unregisterTextFieldViewClass(UITextField.self, didBeginEditingNotificationName: UITextFieldTextDidBeginEditing.rawValue, didEndEditingNotificationName: UITextFieldTextDidEndEditing.rawValue)
  1725. // Unregistering for UITextView notification.
  1726. unregisterTextFieldViewClass(UITextView.self, didBeginEditingNotificationName: UITextViewTextDidBeginEditing.rawValue, didEndEditingNotificationName: UITextViewTextDidEndEditing.rawValue)
  1727. // Unregistering for orientation changes notification
  1728. NotificationCenter.default.removeObserver(self, name: UIApplicationWillChangeStatusBarOrientation, object: UIApplication.shared)
  1729. }
  1730. private func showLog(_ logString: String, indentation: Int = 0) {
  1731. struct Static {
  1732. static var indentation = 0
  1733. }
  1734. if indentation < 0 {
  1735. Static.indentation = max(0, Static.indentation + indentation)
  1736. }
  1737. if enableDebugging {
  1738. var preLog = "IQKeyboardManager"
  1739. for _ in 0 ... Static.indentation {
  1740. preLog += "|\t"
  1741. }
  1742. print(preLog + logString)
  1743. }
  1744. if indentation > 0 {
  1745. Static.indentation += indentation
  1746. }
  1747. }
  1748. }