美文网首页
监听键盘通知

监听键盘通知

作者: zhouios | 来源:发表于2016-09-20 15:57 被阅读0次
    // 监听键盘的位置改变
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    // 监听键盘的通知
    [center addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    // 收到键盘的frame将要改变的通知时会调用此方法
    - (void)keyboardWillChangeFrame:(NSNotification *)note {
        // 拿到键盘弹出或隐藏后的frame
        CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        // 键盘弹出时
        //    667 - 667  = 0
        // 控制器要移动的位置 = 键盘显示后或隐藏后的y  - 控制器的高
        CGFloat transformY = keyboardFrame.origin.y - self.view.bounds.size.height;
        // 让控制器跟随键盘的弹出和隐藏来移动
        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
        
    }
    
    //这个是回到原来的位置
    self.view.transform = CGAffineTransformIdentity;
    

    相关文章

      网友评论

          本文标题:监听键盘通知

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