首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Objective中的NSNotificationCenter实现按键盘在屏幕上显示和隐藏ScrollView(上下键)

用Objective中的NSNotificationCenter实现按键盘在屏幕上显示和隐藏ScrollView(上下键)
EN

Stack Overflow用户
提问于 2017-01-12 11:38:24
回答 2查看 111关注 0票数 1

假设我有一个屏幕,在这个屏幕上我有10个文本字段,现在我的问题是当键盘打开时,键盘在我的3个文本字段上打开。那么我该如何使用UIScrollView和NSNotification中心来管理它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-12 11:49:41

是的,我有一个代码这一点,我相信这肯定是你的工作。

代码语言:javascript
复制
 [[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;
}
票数 1
EN

Stack Overflow用户

发布于 2017-01-12 12:06:17

您可以创建一个UIView类别,使其能够观察UIKeyboardWillShowNotificationUIKeyboardWillHideNotification通知

如下所示

代码语言:javascript
复制
@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

在您的使用中,您只需要导入类别文件,然后设置代码

代码语言:javascript
复制
    [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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41604713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档