-
Notifications
You must be signed in to change notification settings - Fork 0
/
IQUIView+Hierarchy.swift
executable file
·328 lines (241 loc) · 11.2 KB
/
IQUIView+Hierarchy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// IQUIView+Hierarchy.swift
// https://github.com/hackiftekhar/IQKeyboardManager
// Copyright (c) 2013-16 Iftekhar Qurashi.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
/**
UIView hierarchy category.
*/
public extension UIView {
///----------------------
/// MARK: viewControllers
///----------------------
/**
Returns the UIViewController object that manages the receiver.
*/
@objc public func viewContainingController()->UIViewController? {
var nextResponder: UIResponder? = self
repeat {
nextResponder = nextResponder?.next
if let viewController = nextResponder as? UIViewController {
return viewController
}
} while nextResponder != nil
return nil
}
/**
Returns the topMost UIViewController object in hierarchy.
*/
@objc public func topMostController()->UIViewController? {
var controllersHierarchy = [UIViewController]()
if var topController = window?.rootViewController {
controllersHierarchy.append(topController)
while let presented = topController.presentedViewController {
topController = presented
controllersHierarchy.append(presented)
}
var matchController :UIResponder? = viewContainingController()
while matchController != nil && controllersHierarchy.contains(matchController as! UIViewController) == false {
repeat {
matchController = matchController?.next
} while matchController != nil && matchController is UIViewController == false
}
return matchController as? UIViewController
} else {
return viewContainingController()
}
}
/**
Returns the UIViewController object that is actually the parent of this object. Most of the time it's the viewController object which actually contains it, but result may be different if it's viewController is added as childViewController of another viewController.
*/
@objc public func parentContainerViewController()->UIViewController? {
var matchController = viewContainingController()
if var navController = matchController?.navigationController {
while let parentNav = navController.navigationController {
navController = parentNav
}
var parentController : UIViewController = navController
while let parent = parentController.parent,
(parent.isKind(of: UINavigationController.self) == false &&
parent.isKind(of: UITabBarController.self) == false &&
parent.isKind(of: UISplitViewController.self) == false) {
parentController = parent
}
if navController == parentController {
return navController.topViewController
} else {
return parentController
}
}
else if let tabController = matchController?.tabBarController {
if let navController = tabController.selectedViewController as? UINavigationController {
return navController.topViewController
} else {
return tabController.selectedViewController
}
} else {
while let parentController = matchController?.parent,
(parentController.isKind(of: UINavigationController.self) == false &&
parentController.isKind(of: UITabBarController.self) == false &&
parentController.isKind(of: UISplitViewController.self) == false) {
matchController = parentController
}
return matchController;
}
}
///-----------------------------------
/// MARK: Superviews/Subviews/Siglings
///-----------------------------------
/**
Returns the superView of provided class type.
*/
@objc public func superviewOfClassType(_ classType:UIView.Type)->UIView? {
var superView = superview
while let unwrappedSuperView = superView {
if unwrappedSuperView.isKind(of: classType) {
//If it's UIScrollView, then validating for special cases
if unwrappedSuperView.isKind(of: UIScrollView.self) {
let classNameString = NSStringFromClass(type(of:unwrappedSuperView.self))
// If it's not UITableViewWrapperView class, this is internal class which is actually manage in UITableview. The speciality of this class is that it's superview is UITableView.
// If it's not UITableViewCellScrollView class, this is internal class which is actually manage in UITableviewCell. The speciality of this class is that it's superview is UITableViewCell.
//If it's not _UIQueuingScrollView class, actually we validate for _ prefix which usually used by Apple internal classes
if unwrappedSuperView.superview?.isKind(of: UITableView.self) == false &&
unwrappedSuperView.superview?.isKind(of: UITableViewCell.self) == false &&
classNameString.hasPrefix("_") == false {
return superView
}
}
else {
return superView
}
}
superView = unwrappedSuperView.superview
}
return nil
}
/**
Returns all siblings of the receiver which canBecomeFirstResponder.
*/
internal func responderSiblings()->[UIView] {
//Array of (UITextField/UITextView's).
var tempTextFields = [UIView]()
// Getting all siblings
if let siblings = superview?.subviews {
for textField in siblings {
if (textField == self || textField.ignoreSwitchingByNextPrevious == false) && textField._IQcanBecomeFirstResponder() == true {
tempTextFields.append(textField)
}
}
}
return tempTextFields
}
/**
Returns all deep subViews of the receiver which canBecomeFirstResponder.
*/
internal func deepResponderViews()->[UIView] {
//Array of (UITextField/UITextView's).
var textfields = [UIView]()
for textField in subviews {
if (textField == self || textField.ignoreSwitchingByNextPrevious == false) && textField._IQcanBecomeFirstResponder() == true {
textfields.append(textField)
}
//Sometimes there are hidden or disabled views and textField inside them still recorded, so we added some more validations here (Bug ID: #458)
//Uncommented else (Bug ID: #625)
if textField.subviews.count != 0 && isUserInteractionEnabled == true && isHidden == false && alpha != 0.0 {
for deepView in textField.deepResponderViews() {
textfields.append(deepView)
}
}
}
//subviews are returning in opposite order. Sorting according the frames 'y'.
return textfields.sorted(by: { (view1 : UIView, view2 : UIView) -> Bool in
let frame1 = view1.convert(view1.bounds, to: self)
let frame2 = view2.convert(view2.bounds, to: self)
let x1 = frame1.minX
let y1 = frame1.minY
let x2 = frame2.minX
let y2 = frame2.minY
if y1 != y2 {
return y1 < y2
} else {
return x1 < x2
}
})
}
private func _IQcanBecomeFirstResponder() -> Bool {
var _IQcanBecomeFirstResponder = false
// Setting toolbar to keyboard.
if let textField = self as? UITextField {
_IQcanBecomeFirstResponder = textField.isEnabled
} else if let textView = self as? UITextView {
_IQcanBecomeFirstResponder = textView.isEditable
}
if _IQcanBecomeFirstResponder == true {
_IQcanBecomeFirstResponder = isUserInteractionEnabled == true && isHidden == false && alpha != 0.0 && isAlertViewTextField() == false && searchBar() == nil
}
return _IQcanBecomeFirstResponder
}
///-------------------------
/// MARK: Special TextFields
///-------------------------
/**
Returns searchBar if receiver object is UISearchBarTextField, otherwise return nil.
*/
internal func searchBar()-> UISearchBar? {
var responder : UIResponder? = self.next
while let bar = responder {
if let searchBar = bar as? UISearchBar {
return searchBar
} else if bar is UIViewController {
break
}
responder = bar.next
}
return nil
}
/**
Returns YES if the receiver object is UIAlertSheetTextField, otherwise return NO.
*/
internal func isAlertViewTextField()->Bool {
var alertViewController : UIResponder? = viewContainingController()
var isAlertViewTextField = false
while let controller = alertViewController, isAlertViewTextField == false {
if controller.isKind(of: UIAlertController.self) {
isAlertViewTextField = true
break
}
alertViewController = controller.next
}
return isAlertViewTextField
}
private func depth()->Int {
var depth : Int = 0
if let superView = superview {
depth = superView.depth()+1
}
return depth
}
}
extension NSObject {
internal func _IQDescription() -> String {
return "<\(self) \(Unmanaged.passUnretained(self).toOpaque())>"
}
}