IQKeyboardManager+OrientationNotification.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // IQKeyboardManager+OrientationNotification.swift
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-20 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 - UIKit contains Foundation
  24. import UIKit
  25. // MARK: UIStatusBar Notification methods
  26. @available(iOSApplicationExtension, unavailable)
  27. internal extension IQKeyboardManager {
  28. /** UIApplicationWillChangeStatusBarOrientationNotification. Need to set the textView to it's original position. If any frame changes made. (Bug ID: #92)*/
  29. @objc func willChangeStatusBarOrientation(_ notification: Notification) {
  30. let currentStatusBarOrientation: UIInterfaceOrientation
  31. #if swift(>=5.1)
  32. if #available(iOS 13, *) {
  33. currentStatusBarOrientation = keyWindow()?.windowScene?.interfaceOrientation ?? UIInterfaceOrientation.unknown
  34. } else {
  35. currentStatusBarOrientation = UIApplication.shared.statusBarOrientation
  36. }
  37. #else
  38. currentStatusBarOrientation = UIApplication.shared.statusBarOrientation
  39. #endif
  40. guard let statusBarOrientation = notification.userInfo?[UIApplication.statusBarOrientationUserInfoKey] as? Int, currentStatusBarOrientation.rawValue != statusBarOrientation else {
  41. return
  42. }
  43. let startTime = CACurrentMediaTime()
  44. showLog("📱>>>>> \(#function) started >>>>>", indentation: 1)
  45. showLog("Notification Object:\(notification.object ?? "NULL")")
  46. //If textViewContentInsetChanged is saved then restore it.
  47. if let textView = textFieldView as? UIScrollView, textView.responds(to: #selector(getter: UITextView.isEditable)) {
  48. if isTextViewContentInsetChanged {
  49. self.isTextViewContentInsetChanged = false
  50. if textView.contentInset != self.startingTextViewContentInsets {
  51. UIView.animate(withDuration: animationDuration, delay: 0, options: animationCurve, animations: { () -> Void in
  52. self.showLog("Restoring textView.contentInset to: \(self.startingTextViewContentInsets)")
  53. //Setting textField to it's initial contentInset
  54. textView.contentInset = self.startingTextViewContentInsets
  55. textView.scrollIndicatorInsets = self.startingTextViewScrollIndicatorInsets
  56. }, completion: { (_) -> Void in })
  57. }
  58. }
  59. }
  60. restorePosition()
  61. let elapsedTime = CACurrentMediaTime() - startTime
  62. showLog("📱<<<<< \(#function) ended: \(elapsedTime) seconds <<<<<", indentation: -1)
  63. }
  64. }