IQUIScrollView+Additions.swift 3.8 KB

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