IQUIScrollView+Additions.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // IQUIScrollView+Additions.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 UIKit
  25. private var kIQShouldIgnoreScrollingAdjustment = "kIQShouldIgnoreScrollingAdjustment"
  26. private var kIQShouldIgnoreContentInsetAdjustment = "kIQShouldIgnoreContentInsetAdjustment"
  27. private var kIQShouldRestoreScrollViewContentOffset = "kIQShouldRestoreScrollViewContentOffset"
  28. @objc public extension UIScrollView {
  29. /**
  30. If YES, then scrollview will ignore scrolling (simply not scroll it) for adjusting textfield position. Default is NO.
  31. */
  32. @objc var shouldIgnoreScrollingAdjustment: Bool {
  33. get {
  34. if let aValue = objc_getAssociatedObject(self, &kIQShouldIgnoreScrollingAdjustment) as? Bool {
  35. return aValue
  36. } else {
  37. return false
  38. }
  39. }
  40. set(newValue) {
  41. objc_setAssociatedObject(self, &kIQShouldIgnoreScrollingAdjustment, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  42. }
  43. }
  44. /**
  45. If YES, then scrollview will ignore content inset adjustment (simply not updating it) when keyboard is shown. Default is NO.
  46. */
  47. @objc var shouldIgnoreContentInsetAdjustment: Bool {
  48. get {
  49. if let aValue = objc_getAssociatedObject(self, &kIQShouldIgnoreContentInsetAdjustment) as? Bool {
  50. return aValue
  51. } else {
  52. return false
  53. }
  54. }
  55. set(newValue) {
  56. objc_setAssociatedObject(self, &kIQShouldIgnoreContentInsetAdjustment, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  57. }
  58. }
  59. /**
  60. To set customized distance from keyboard for textField/textView. Can't be less than zero
  61. */
  62. @objc var shouldRestoreScrollViewContentOffset: Bool {
  63. get {
  64. if let aValue = objc_getAssociatedObject(self, &kIQShouldRestoreScrollViewContentOffset) as? Bool {
  65. return aValue
  66. } else {
  67. return false
  68. }
  69. }
  70. set(newValue) {
  71. objc_setAssociatedObject(self, &kIQShouldRestoreScrollViewContentOffset, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  72. }
  73. }
  74. }
  75. internal extension UITableView {
  76. func previousIndexPath(of indexPath: IndexPath) -> IndexPath? {
  77. var previousRow = indexPath.row - 1
  78. var previousSection = indexPath.section
  79. //Fixing indexPath
  80. if previousRow < 0 {
  81. previousSection -= 1
  82. if previousSection >= 0 {
  83. previousRow = self.numberOfRows(inSection: previousSection) - 1
  84. }
  85. }
  86. if previousRow >= 0 && previousSection >= 0 {
  87. return IndexPath(row: previousRow, section: previousSection)
  88. } else {
  89. return nil
  90. }
  91. }
  92. }
  93. internal extension UICollectionView {
  94. func previousIndexPath(of indexPath: IndexPath) -> IndexPath? {
  95. var previousRow = indexPath.row - 1
  96. var previousSection = indexPath.section
  97. //Fixing indexPath
  98. if previousRow < 0 {
  99. previousSection -= 1
  100. if previousSection >= 0 {
  101. previousRow = self.numberOfItems(inSection: previousSection) - 1
  102. }
  103. }
  104. if previousRow >= 0 && previousSection >= 0 {
  105. return IndexPath(item: previousRow, section: previousSection)
  106. } else {
  107. return nil
  108. }
  109. }
  110. }