键盘弹起

- (void)viewWillAppear:(BOOL)animated
{
    [[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
{
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardFrame.origin.y;
    
    // 计算视图需要移动的距离
    CGFloat space = self.imageView.frame.origin.y + self.imageView.frame.size.height;
    
    // 得出键盘距离输入框的间距
    CGFloat trsformY = height - space;
    
    if (trsformY < 0) {
        CGRect frame = self.view.frame;
        frame.origin.y = trsformY;
        self.view.frame = frame;
    }
    
}

// 键盘隐藏
- (void)keyboardWillHide:(NSNotification *)notification
{
    CGRect frame = self.view.frame;
    frame.origin.y = 64;
    self.view.frame = frame;
}

 

posted @ 2016-11-30 17:24  唐宋元明清。  阅读(149)  评论(0编辑  收藏  举报