美文网首页
键盘监听与IQKeyboardManager配合使用

键盘监听与IQKeyboardManager配合使用

作者: 要成为_海贼王_的男人 | 来源:发表于2018-07-07 10:41 被阅读0次

    项目中做了一个底部输入框跟随键盘弹起上移收起返回,只用IQKeyboardManager的时候系统键盘和三方键盘切换的时候底部输入框就自动回收了,而且系统键盘的时候底部输入框弹不起来所以给键盘添加监听以后问题就解决了

    //监听当键盘将要出现时
        [[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 *)notification
    {
        //获取键盘的高度
        NSDictionary *userInfo = [notification userInfo];
        NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [value CGRectValue];
        int height = keyboardRect.size.height;
        
        [self.view bringSubviewToFront:self.scrollView];
        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = self.bottomView.frame;
            frame.origin.y = self.view.height - height - SKWidth(46);
            self.bottomView.frame = frame;
        }];
    }
    
    //当键退出
    - (void)keyboardWillHide:(NSNotification *)notification
    {
        //获取键盘的高度
        NSDictionary *userInfo = [notification userInfo];
        NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [value CGRectValue];
        int height = keyboardRect.size.height;
        if (height == 0) {
            
            [self.view sendSubviewToBack:self.scrollView];
            
        }
        
        [self.view bringSubviewToFront:self.scrollView];
        [UIView animateWithDuration:0.5 animations:^{
            CGRect frame = self.bottomView.frame;
            frame.origin.y = self.view.height - self.bottomView.height;
            self.bottomView.frame = frame;
        }];
    }
    

    相关文章

      网友评论

          本文标题:键盘监听与IQKeyboardManager配合使用

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