Skip to content

Commit 333f52f

Browse files
Mohd Iftekhar QurashiMohd Iftekhar Qurashi
authored andcommitted
Fixed UITtextView, UIWebView issue. Toolbar category changed from UITextField to UIView, Fixed Xcode 4.2 compilation issue, Changed notification from UIKeyboardDidShow to UIkeyboardWillShow, Added WebViewController
1 parent 2cba109 commit 333f52f

File tree

11 files changed

+406
-195
lines changed

11 files changed

+406
-195
lines changed

KeyboardTextFieldDemo/IQKeyBoardManager/IQKeyBoardManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363

6464
/*****************UITextField***********************/
65-
@interface UITextField (ToolbarOnKeyboard)
65+
@interface UIView (ToolbarOnKeyboard)
6666

6767
//Helper functions to add Done button on keyboard.
6868
-(void)addDoneOnKeyboardWithTarget:(id)target action:(SEL)action;

KeyboardTextFieldDemo/IQKeyBoardManager/IQKeyBoardManager.m

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ @interface IQKeyBoardManager()
103103
@property(nonatomic, assign) CGFloat keyboardDistanceFromTextField;
104104
@property(nonatomic, assign) BOOL isEnabled;
105105

106+
-(void)adjustFrameWithDuration:(CGFloat)aDuration;
107+
-(void)commonDidBeginEditing;
108+
106109
@end
107110

108111
@implementation IQKeyBoardManager
@@ -137,7 +140,7 @@ +(void)enableKeyboardManger
137140
{
138141
kbManager.isEnabled = YES;
139142
/*Registering for keyboard notification*/
140-
[[NSNotificationCenter defaultCenter] addObserver:kbManager selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
143+
[[NSNotificationCenter defaultCenter] addObserver:kbManager selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
141144
[[NSNotificationCenter defaultCenter] addObserver:kbManager selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
142145

143146
/*Registering for textField notification*/
@@ -226,6 +229,11 @@ -(void)setRootViewFrame:(CGRect)frame
226229
// Keyboard Will hide. So setting rootViewController to it's default frame.
227230
- (void)keyboardWillHide:(NSNotification*)aNotification
228231
{
232+
if (textFieldView == nil)
233+
{
234+
return;
235+
}
236+
229237
//Boolean to know keyboard is showing/hiding
230238
isKeyboardShowing = NO;
231239

@@ -241,8 +249,8 @@ - (void)keyboardWillHide:(NSNotification*)aNotification
241249
[self setRootViewFrame:topViewBeginRect];
242250
}
243251

244-
//UIKeyboard Did show
245-
-(void)keyboardDidShow:(NSNotification*)aNotification
252+
//UIKeyboard Will show
253+
-(void)keyboardWillShow:(NSNotification*)aNotification
246254
{
247255
//Getting keyboard animation duration
248256
CGFloat duration = [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
@@ -273,7 +281,13 @@ -(void)keyboardDidShow:(NSNotification*)aNotification
273281
}
274282

275283
//UIKeyboard Did show. Adjusting RootViewController's frame according to device orientation.
276-
-(void)adjustFrameWithDuration:(CGFloat)aDuration {
284+
-(void)adjustFrameWithDuration:(CGFloat)aDuration
285+
{
286+
if (textFieldView == nil)
287+
{
288+
return;
289+
}
290+
277291
//Boolean to know keyboard is showing/hiding
278292
isKeyboardShowing = YES;
279293

@@ -435,7 +449,8 @@ -(void)textViewdDidEndEditing:(NSNotification*)notification
435449
}
436450

437451
// Common code to perform on begin editing
438-
-(void)commonDidBeginEditing {
452+
-(void)commonDidBeginEditing
453+
{
439454
if (isKeyboardShowing)
440455
{
441456
// keyboard is already showing. adjust frame.
@@ -446,14 +461,17 @@ -(void)commonDidBeginEditing {
446461
//keyboard is not showing(At the beginning only). We should save rootViewRect.
447462
UIViewController *rootController = [IQKeyBoardManager topMostController];
448463
topViewBeginRect = rootController.view.frame;
464+
465+
// keyboard is not showing. adjust frame.
466+
[self adjustFrameWithDuration:animationDuration];
449467
}
450468
}
451469

452470
@end
453471

454472

455473
/*Additional Function*/
456-
@implementation UITextField(ToolbarOnKeyboard)
474+
@implementation UIView (ToolbarOnKeyboard)
457475

458476
#pragma mark - Toolbar on UIKeyboard
459477
-(void)addDoneOnKeyboardWithTarget:(id)target action:(SEL)action
@@ -472,8 +490,11 @@ -(void)addDoneOnKeyboardWithTarget:(id)target action:(SEL)action
472490
//Adding button to toolBar.
473491
[toolbar setItems:[NSArray arrayWithObjects: nilButton,doneButton, nil]];
474492

475-
//Setting toolbar to textFieldPhoneNumber keyboard.
476-
[self setInputAccessoryView:toolbar];
493+
if ([self respondsToSelector:@selector(setInputAccessoryView:)])
494+
{
495+
//Setting toolbar to textFieldPhoneNumber keyboard.
496+
[(UITextField*)self setInputAccessoryView:toolbar];
497+
}
477498
}
478499

479500
-(void)addPreviousNextDoneOnKeyboardWithTarget:(id)target previousAction:(SEL)previousAction nextAction:(SEL)nextAction doneAction:(SEL)doneAction
@@ -497,8 +518,11 @@ -(void)addPreviousNextDoneOnKeyboardWithTarget:(id)target previousAction:(SEL)pr
497518
[toolbar setItems:[NSArray arrayWithObjects: segButton,nilButton,doneButton, nil]];
498519
// [toolbar setItems:[NSArray arrayWithObjects: previousButton,nextButton,nilButton,doneButton, nil]];
499520

500-
//Setting toolbar to textFieldPhoneNumber keyboard.
501-
[self setInputAccessoryView:toolbar];
521+
if ([self respondsToSelector:@selector(setInputAccessoryView:)])
522+
{
523+
//Setting toolbar to textFieldPhoneNumber keyboard.
524+
[(UITextField*)self setInputAccessoryView:toolbar];
525+
}
502526
}
503527

504528
-(void)setEnablePrevious:(BOOL)isPreviousEnabled next:(BOOL)isNextEnabled

KeyboardTextFieldDemo/KeyboardTextFieldDemo.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
C0B63BAC1781FAB1008D3B64 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C0B63BAB1781FAB1008D3B64 /* main.m */; };
1717
C0B63BB01781FAB1008D3B64 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C0B63BAF1781FAB1008D3B64 /* AppDelegate.m */; };
1818
C0B63BB31781FAB1008D3B64 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C0B63BB21781FAB1008D3B64 /* ViewController.m */; };
19+
C0DB7D0A184FBA6000F5C892 /* WebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C0DB7D08184FBA6000F5C892 /* WebViewController.m */; };
20+
C0DB7D0B184FBA6000F5C892 /* WebViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C0DB7D09184FBA6000F5C892 /* WebViewController.xib */; };
1921
C0FAECC817F2351700962F68 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C0FAECC717F2351700962F68 /* ViewController.xib */; };
2022
/* End PBXBuildFile section */
2123

@@ -35,6 +37,9 @@
3537
C0B63BAF1781FAB1008D3B64 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
3638
C0B63BB11781FAB1008D3B64 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
3739
C0B63BB21781FAB1008D3B64 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
40+
C0DB7D07184FBA6000F5C892 /* WebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebViewController.h; sourceTree = "<group>"; };
41+
C0DB7D08184FBA6000F5C892 /* WebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebViewController.m; sourceTree = "<group>"; };
42+
C0DB7D09184FBA6000F5C892 /* WebViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = WebViewController.xib; sourceTree = "<group>"; };
3843
C0FAECC717F2351700962F68 /* ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ViewController.xib; sourceTree = "<group>"; };
3944
/* End PBXFileReference section */
4045

@@ -98,6 +103,9 @@
98103
C0B63BB11781FAB1008D3B64 /* ViewController.h */,
99104
C0B63BB21781FAB1008D3B64 /* ViewController.m */,
100105
C0FAECC717F2351700962F68 /* ViewController.xib */,
106+
C0DB7D07184FBA6000F5C892 /* WebViewController.h */,
107+
C0DB7D08184FBA6000F5C892 /* WebViewController.m */,
108+
C0DB7D09184FBA6000F5C892 /* WebViewController.xib */,
101109
C0B63BA61781FAB1008D3B64 /* Supporting Files */,
102110
);
103111
path = KeyboardTextFieldDemo;
@@ -168,6 +176,7 @@
168176
C0B63BAA1781FAB1008D3B64 /* InfoPlist.strings in Resources */,
169177
AF4301BA179E92C400FADAC6 /* Default-568h@2x.png in Resources */,
170178
C0FAECC817F2351700962F68 /* ViewController.xib in Resources */,
179+
C0DB7D0B184FBA6000F5C892 /* WebViewController.xib in Resources */,
171180
);
172181
runOnlyForDeploymentPostprocessing = 0;
173182
};
@@ -182,6 +191,7 @@
182191
C0B63BB01781FAB1008D3B64 /* AppDelegate.m in Sources */,
183192
C0B63BB31781FAB1008D3B64 /* ViewController.m in Sources */,
184193
AFF236C117CA224400760F6C /* IQKeyBoardManager.m in Sources */,
194+
C0DB7D0A184FBA6000F5C892 /* WebViewController.m in Sources */,
185195
);
186196
runOnlyForDeploymentPostprocessing = 0;
187197
};

KeyboardTextFieldDemo/KeyboardTextFieldDemo/AppDelegate.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
#import <UIKit/UIKit.h>
77

8-
@class ViewController;
9-
108
@interface AppDelegate : UIResponder <UIApplicationDelegate>
119

1210
@property (strong, nonatomic) UIWindow *window;
1311

14-
@property (strong, nonatomic) ViewController *viewController;
12+
@property (strong, nonatomic) UIViewController *viewController;
1513

1614
@end

KeyboardTextFieldDemo/KeyboardTextFieldDemo/AppDelegate.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import "AppDelegate.h"
88
#import "IQKeyBoardManager.h"
99
#import "ViewController.h"
10+
#import "WebViewController.h"
1011

1112
@implementation AppDelegate
1213

@@ -18,10 +19,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
1819
[IQKeyBoardManager installKeyboardManager];
1920

2021
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
21-
// Override point for customization after application launch.
22-
2322

24-
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
23+
//Uncomment-Comment this line to test on WebView.
24+
self.viewController = [[ViewController alloc] init];
25+
// self.viewController = [[WebViewController alloc] init];
2526

2627
//Case 1. UIViewController as rootViewController.
2728
if (false)

KeyboardTextFieldDemo/KeyboardTextFieldDemo/ViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#import <UIKit/UIKit.h>
88

9-
@interface ViewController : UIViewController<UITextFieldDelegate>
9+
@interface ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>
1010
{
1111
NSInteger selectedTextFieldTag;
1212

KeyboardTextFieldDemo/KeyboardTextFieldDemo/ViewController.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ -(void)disableKeyboardManager:(UIBarButtonItem*)barButton
7373
[IQKeyBoardManager disableKeyboardManager];
7474
}
7575

76-
7776
-(void)previousClicked:(UISegmentedControl*)segmentedControl
7877
{
7978
[(UITextField*)[self.view viewWithTag:selectedTextFieldTag-1] becomeFirstResponder];
@@ -127,6 +126,11 @@ - (BOOL)shouldAutorotate{
127126
// return UIInterfaceOrientationMaskAll;
128127
//}
129128

129+
-(void)textViewDidBeginEditing:(UITextView *)textView
130+
{
131+
selectedTextFieldTag = textView.tag;
132+
}
133+
130134
-(void)textFieldDidBeginEditing:(UITextField *)textField
131135
{
132136
selectedTextFieldTag = textField.tag;

0 commit comments

Comments
 (0)