iOS中改变键盘的高度 监听键盘

来源:互联网 发布:mac电脑分享wifi给手机 编辑:程序博客网 时间:2024/05/18 09:24
//添加键盘通知    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];#pragma mark - 私有方法//其次键盘的高度计算:-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度{        CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];        CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];        return keyboardEndingFrame.size.height;    }//键盘将要弹出时,改变高度-(void)keyboardWillAppear:(NSNotification *)notification{        CGRect currentFrame = self.tableView.frame;        CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];        currentFrame.origin.y = currentFrame.origin.y - change;        self.tableView.frame = currentFrame;    }//最后,当键盘消失后,视图需要恢复原状。-(void)keyboardWillDisappear:(NSNotification *)notification{        CGRect currentFrame = self.tableView.frame;        CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];        currentFrame.origin.y = currentFrame.origin.y + change;        self.tableView.frame = currentFrame;    }

原创粉丝点击