91c253433b4cc0b1bf4df040dae64a9fd61be02c
[mirrors/ShenYin.git] /
1 //
2 //  UIScrollView+IQKeyboardManagerExtension.swift
3 //  https://github.com/hackiftekhar/IQKeyboardManager
4 //  Copyright (c) 2013-24 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
24 import UIKit
25 import IQKeyboardCore
26
27 @available(iOSApplicationExtension, unavailable)
28 @MainActor
29 private struct AssociatedKeys {
30     static var ignoreScrollingAdjustment: Int = 0
31     static var ignoreContentInsetAdjustment: Int = 0
32     static var restoreContentOffset: Int = 0
33 }
34
35 @available(iOSApplicationExtension, unavailable)
36 @MainActor
37 public extension IQKeyboardExtension where Base: UIScrollView {
38
39     /**
40      If YES, then scrollview will ignore scrolling (simply not scroll it) for adjusting textInputView position.
41      Default is NO.
42      */
43     var ignoreScrollingAdjustment: Bool {
44         get {
45             if let base = base {
46                 return objc_getAssociatedObject(base, &AssociatedKeys.ignoreScrollingAdjustment) as? Bool ?? false
47             }
48             return false
49         }
50         set(newValue) {
51             if let base = base {
52                 objc_setAssociatedObject(base, &AssociatedKeys.ignoreScrollingAdjustment,
53                                          newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
54             }
55         }
56     }
57
58     /**
59      If YES, then scrollview will ignore content inset adjustment (simply not updating it) when keyboard is shown.
60      Default is NO.
61      */
62     var ignoreContentInsetAdjustment: Bool {
63         get {
64             if let base = base {
65                 return objc_getAssociatedObject(base, &AssociatedKeys.ignoreContentInsetAdjustment) as? Bool ?? false
66             }
67             return false
68         }
69         set(newValue) {
70             if let base = base {
71                 objc_setAssociatedObject(base, &AssociatedKeys.ignoreContentInsetAdjustment,
72                                          newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
73             }
74         }
75     }
76
77     /**
78      If we should restore scrollView contentOffset to it's initial position
79      */
80     var restoreContentOffset: Bool {
81         get {
82             if let base = base {
83                 return objc_getAssociatedObject(base, &AssociatedKeys.restoreContentOffset) as? Bool ?? false
84             }
85             return false
86         }
87         set(newValue) {
88             if let base = base {
89                 objc_setAssociatedObject(base, &AssociatedKeys.restoreContentOffset,
90                                          newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
91             }
92         }
93     }
94 }
95
96 // swiftlint:disable unused_setter_value
97 @available(iOSApplicationExtension, unavailable)
98 @MainActor
99 @objc public extension UIScrollView {
100     @available(*, unavailable, renamed: "iq.ignoreScrollingAdjustment")
101     var shouldIgnoreScrollingAdjustment: Bool {
102         get { false }
103         set { }
104     }
105
106     @available(*, unavailable, renamed: "iq.ignoreContentInsetAdjustment")
107     var shouldIgnoreContentInsetAdjustment: Bool {
108         get { false }
109         set { }
110     }
111
112     @available(*, unavailable, renamed: "iq.restoreContentOffset")
113     var shouldRestoreScrollViewContentOffset: Bool {
114         get { false }
115         set { }
116     }
117 }
118 // swiftlint:enable unused_setter_value