Skip to content

Commit 42e2c0c

Browse files
committed
Added ignoreSwitchingByNextPrevious to ignore any textField with previous/next button navigation, however user can still tap on the textField to become first responder.
1 parent 522f0ed commit 42e2c0c

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

IQKeyboardManager/Categories/IQUITextFieldView+Additions.h

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
*/
3535
@property(nonatomic, assign) CGFloat keyboardDistanceFromTextField;
3636

37+
/**
38+
If shouldIgnoreSwitchingByNextPrevious is YES then library will ignore this textField/textView while moving to other textField/textView using keyboard toolbar next previous buttons. Default is NO
39+
*/
40+
@property(nonatomic, assign) BOOL ignoreSwitchingByNextPrevious;
41+
3742
@end
3843

3944
///-------------------------------------------

IQKeyboardManager/Categories/IQUITextFieldView+Additions.m

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ -(void)setKeyboardDistanceFromTextField:(CGFloat)keyboardDistanceFromTextField
3131
//Can't be less than zero. Minimum is zero.
3232
keyboardDistanceFromTextField = MAX(keyboardDistanceFromTextField, 0);
3333

34-
objc_setAssociatedObject(self, @selector(keyboardDistanceFromTextField), [NSNumber numberWithFloat:keyboardDistanceFromTextField], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
34+
objc_setAssociatedObject(self, @selector(keyboardDistanceFromTextField), @(keyboardDistanceFromTextField), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
3535
}
3636

3737
-(CGFloat)keyboardDistanceFromTextField
@@ -41,6 +41,18 @@ -(CGFloat)keyboardDistanceFromTextField
4141
return (keyboardDistanceFromTextField)?[keyboardDistanceFromTextField floatValue]:kIQUseDefaultKeyboardDistance;
4242
}
4343

44+
-(void)setIgnoreSwitchingByNextPrevious:(BOOL)ignoreSwitchingByNextPrevious
45+
{
46+
objc_setAssociatedObject(self, @selector(ignoreSwitchingByNextPrevious), @(ignoreSwitchingByNextPrevious), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
47+
}
48+
49+
-(BOOL)ignoreSwitchingByNextPrevious
50+
{
51+
NSNumber *ignoreSwitchingByNextPrevious = objc_getAssociatedObject(self, @selector(ignoreSwitchingByNextPrevious));
52+
53+
return [ignoreSwitchingByNextPrevious boolValue];
54+
}
55+
4456
@end
4557

4658
///------------------------------------

IQKeyboardManager/Categories/IQUIView+Hierarchy.m

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// THE SOFTWARE.
2323

2424
#import "IQUIView+Hierarchy.h"
25+
#import "IQUITextFieldView+Additions.h"
2526

2627
#import <UIKit/UICollectionView.h>
2728
#import <UIKit/UIAlertController.h>
@@ -150,7 +151,7 @@ - (NSArray*)responderSiblings
150151
NSMutableArray<UIView*> *tempTextFields = [[NSMutableArray alloc] init];
151152

152153
for (UIView *textField in siblings)
153-
if ([textField _IQcanBecomeFirstResponder])
154+
if ((textField == self || textField.ignoreSwitchingByNextPrevious == NO) && [textField _IQcanBecomeFirstResponder])
154155
[tempTextFields addObject:textField];
155156

156157
return tempTextFields;
@@ -162,7 +163,7 @@ - (NSArray*)deepResponderViews
162163

163164
for (UIView *textField in self.subviews)
164165
{
165-
if ([textField _IQcanBecomeFirstResponder])
166+
if ((textField == self || textField.ignoreSwitchingByNextPrevious == NO) && [textField _IQcanBecomeFirstResponder])
166167
{
167168
[textFields addObject:textField];
168169
}

IQKeyboardManagerSwift/Categories/IQUITextFieldView+Additions.swift

+20-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ Uses default keyboard distance for textField.
3030
public let kIQUseDefaultKeyboardDistance = CGFloat.greatestFiniteMagnitude
3131

3232
private var kIQKeyboardDistanceFromTextField = "kIQKeyboardDistanceFromTextField"
33+
private var kIQIgnoreSwitchingByNextPrevious = "kIQIgnoreSwitchingByNextPrevious"
3334

3435
/**
3536
UIView category for managing UITextField/UITextView
3637
*/
3738
public extension UIView {
3839

3940
/**
40-
To set customized distance from keyboard for textField/textView. Can't be less than zero
41-
*/
41+
To set customized distance from keyboard for textField/textView. Can't be less than zero
42+
*/
4243
public var keyboardDistanceFromTextField: CGFloat {
4344
get {
4445

@@ -52,5 +53,22 @@ public extension UIView {
5253
objc_setAssociatedObject(self, &kIQKeyboardDistanceFromTextField, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
5354
}
5455
}
56+
57+
/**
58+
If shouldIgnoreSwitchingByNextPrevious is true then library will ignore this textField/textView while moving to other textField/textView using keyboard toolbar next previous buttons. Default is false
59+
*/
60+
public var ignoreSwitchingByNextPrevious: Bool {
61+
get {
62+
63+
if let aValue = objc_getAssociatedObject(self, &kIQIgnoreSwitchingByNextPrevious) as? Bool {
64+
return aValue
65+
} else {
66+
return false
67+
}
68+
}
69+
set(newValue) {
70+
objc_setAssociatedObject(self, &kIQIgnoreSwitchingByNextPrevious, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
71+
}
72+
}
5573
}
5674

IQKeyboardManagerSwift/Categories/IQUIView+Hierarchy.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public extension UIView {
140140

141141
for textField in siblings {
142142

143-
if textField._IQcanBecomeFirstResponder() == true {
143+
if (textField == self || textField.ignoreSwitchingByNextPrevious == false) && textField._IQcanBecomeFirstResponder() == true {
144144
tempTextFields.append(textField)
145145
}
146146
}
@@ -159,7 +159,7 @@ public extension UIView {
159159

160160
for textField in subviews {
161161

162-
if textField._IQcanBecomeFirstResponder() == true {
162+
if (textField == self || textField.ignoreSwitchingByNextPrevious == false) && textField._IQcanBecomeFirstResponder() == true {
163163
textfields.append(textField)
164164
}
165165

Podfile.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PODS:
22
- IQDropDownTextField (1.1.0)
3-
- IQKeyboardManager (5.0.0)
4-
- IQKeyboardManagerSwift (5.0.0)
3+
- IQKeyboardManager (5.0.2)
4+
- IQKeyboardManagerSwift (5.0.2)
55
- YYText (1.0.7)
66

77
DEPENDENCIES:
@@ -18,8 +18,8 @@ EXTERNAL SOURCES:
1818

1919
SPEC CHECKSUMS:
2020
IQDropDownTextField: 614f79ca905529c992045eaa112d0fc9ebb875d1
21-
IQKeyboardManager: 5edc5bcd5cd3e6982924e6318b82c57099eb0a6c
22-
IQKeyboardManagerSwift: 9551c6f5456cb7dafbb395aac38f49f2b38c8f89
21+
IQKeyboardManager: 5ab84749f20f682f784160c2c63bef4f19d10d26
22+
IQKeyboardManagerSwift: 3fa8338b21751dffbae93dd78198a6b7aef43406
2323
YYText: 5c461d709e24d55a182d1441c41dc639a18a4849
2424

2525
PODFILE CHECKSUM: f0cfee8989504b0e144cce04af943f540a0a24f1

0 commit comments

Comments
 (0)