美文网首页
监听键盘的弹出和收回通知

监听键盘的弹出和收回通知

作者: ToBeABetter_man | 来源:发表于2018-09-17 19:36 被阅读29次
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //增加监听,当键盘出现或改变时触发方法
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
        //增加监听,当键退出时调用代理方法
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];    
    }
    
     //当键盘出现或改变时回调的代理方法
    - (void)keyboardWillShow:(NSNotification *)aNotification
    {
        //获取键盘的高度
        NSDictionary *userInfo = [aNotification userInfo];
        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [aValue CGRectValue];
       int height = keyboardRect.size.height;
    }
    
    //当键退出时回调
    - (void)keyboardWillHide:(NSNotification *)aNotification{}
    
    

    相关文章

      网友评论

          本文标题:监听键盘的弹出和收回通知

          本文链接:https://www.haomeiwen.com/subject/ughdnftx.html