2 // IQScrollViewConfiguration.swift
3 // https://github.com/hackiftekhar/IQKeyboardManager
4 // Copyright (c) 2013-24 Iftekhar Qurashi.
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
26 @available(iOSApplicationExtension, unavailable)
28 internal struct IQScrollViewConfiguration {
29 let scrollView: UIScrollView
30 let startingContentOffset: CGPoint
31 let startingScrollIndicatorInsets: UIEdgeInsets
32 let startingContentInset: UIEdgeInsets
34 private let canRestoreContentOffset: Bool
36 init(scrollView: UIScrollView, canRestoreContentOffset: Bool) {
37 self.scrollView = scrollView
38 self.canRestoreContentOffset = canRestoreContentOffset
40 startingContentOffset = scrollView.contentOffset
41 startingContentInset = scrollView.contentInset
44 if #available(iOS 11.1, *) {
45 startingScrollIndicatorInsets = scrollView.verticalScrollIndicatorInsets
47 startingScrollIndicatorInsets = scrollView.scrollIndicatorInsets
50 startingScrollIndicatorInsets = scrollView.scrollIndicatorInsets
54 var hasChanged: Bool {
55 if scrollView.contentInset != self.startingContentInset {
59 if canRestoreContentOffset,
60 scrollView.iq.restoreContentOffset,
61 !scrollView.contentOffset.equalTo(startingContentOffset) {
68 func restore(for textFieldView: UIView?) -> Bool {
69 var success: Bool = false
71 if scrollView.contentInset != self.startingContentInset {
72 scrollView.contentInset = self.startingContentInset
73 scrollView.layoutIfNeeded() // (Bug ID: #1996)
78 if #available(iOS 11.1, *) {
79 if scrollView.verticalScrollIndicatorInsets != self.startingScrollIndicatorInsets {
80 scrollView.verticalScrollIndicatorInsets = self.startingScrollIndicatorInsets
83 if scrollView.scrollIndicatorInsets != self.startingScrollIndicatorInsets {
84 scrollView.scrollIndicatorInsets = self.startingScrollIndicatorInsets
88 if scrollView.scrollIndicatorInsets != self.startingScrollIndicatorInsets {
89 scrollView.scrollIndicatorInsets = self.startingScrollIndicatorInsets
93 if canRestoreContentOffset,
94 scrollView.iq.restoreContentOffset,
95 !scrollView.contentOffset.equalTo(startingContentOffset) {
97 // (Bug ID: #1365, #1508, #1541)
98 let stackView: UIStackView? = textFieldView?.iq.superviewOf(type: UIStackView.self,
99 belowView: scrollView)
100 // (Bug ID: #1901, #1996)
101 let animatedContentOffset: Bool = stackView != nil ||
102 scrollView is UICollectionView ||
103 scrollView is UITableView
105 if animatedContentOffset {
106 scrollView.setContentOffset(startingContentOffset, animated: UIView.areAnimationsEnabled)
108 scrollView.contentOffset = startingContentOffset