假设我有一个屏幕,在这个屏幕上我有10个文本字段,现在我的问题是当键盘打开时,键盘在我的3个文本字段上打开。那么我该如何使用UIScrollView和NSNotification中心来管理它呢?
发布于 2017-01-12 11:49:41
是的,我有一个代码这一点,我相信这肯定是你的工作。
[[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;
}发布于 2017-01-12 12:06:17
您可以创建一个UIView类别,使其能够观察UIKeyboardWillShowNotification和UIKeyboardWillHideNotification通知
如下所示
@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在您的使用中,您只需要导入类别文件,然后设置代码
[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];https://stackoverflow.com/questions/41604713
复制相似问题