Reputation: 19
Consider I have one screen, and in this screen I have 10 textfields, now my issue is when keyboard is open then keyboard is up on my 3 textfield. so how can i manage this using UIScrollView and NSNotification Center?
Upvotes: 1
Views: 124
Reputation: 181
You can create a category of UIView to make it has capability to Observe the UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
Notification
like below
@implementation UIView (PFInputViewControl)
-(void)setupKeyboardShow:(void(^)(NSNotification *notifacation))showblock hide:(void (^)(NSNotification *notifacation))hideblock {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQuene = [NSOperationQueue mainQueue];
[notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:showblock];
[notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:hideblock];
//Setting tap hid gesture when the keyboard is showed and remove the tap gesture when it disappear
[self setupForDismissKeyboard];
}
-(void)setupForDismissKeyboard{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHidAction)];
__weak typeof(self)weakSelf = self;
NSOperationQueue *mainQuene = [NSOperationQueue mainQueue];
[notificationCenter addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQuene usingBlock:^(NSNotification * _Nonnull note) {
[weakSelf addGestureRecognizer:singleTap];
}];
[notificationCenter addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQuene usingBlock:^(NSNotification * _Nonnull note) {
[weakSelf removeGestureRecognizer:singleTap];
}];
}
@end
And in your usage ,you just need to import the category file,and then setup the code
[self.view setupKeyboardShow:^(NSNotification *notification) {
//========== do your action when keyboard is show ==========
//....
//
//such as
NSDictionary* info = [notification userInfo];
//get the size of key board
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//make transform when key board show
self.view.y = - kbSize.height;
} hide:^(NSNotification *notification) {
//.... key board hid action
self.view.y = 0;
//....
}];
//setup keyboard dismiss when touch the screen out side the keyboard
[self.view setupForDismissKeyboard];
Upvotes: 0
Reputation: 61
Yes I have One Code For this, I am Sure its Definitely Work For you.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
MyScrollView.contentInset = contentInsets;
MyScrollView.scrollIndicatorInsets = contentInsets;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
MyScrollView.contentInset = contentInsets;
MyScrollView.scrollIndicatorInsets = contentInsets;
}
Upvotes: 1