动态调整textView的高度,不被keyboard遮盖

1,动态的调整textView的高度,从而不被keyboard所遮盖,只要知道keyboard的高度即可相应的设置textView的高度。

2,如果获取keyboard的高度?

    我们可以通过监听keyboard显示通知,从通知的userInfo中可以获取相关的数据信息,具体代码如下:

 //监听keyboard显示事件
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];

 

//键盘显示后,对textView的高度的处理
- (void)keyboardShow:(NSNotification *)notification
{
    NSValue *value = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [value CGRectValue];
    NSLog(@"%@",self.view);
    [self animationForTextViewHeight:self.view.frame.size.height - keyboardFrame.size.height - self.MyTextView.frame.origin.y -5];
}

 其中userInfo中的数据是一个字典,对应于keyboard高度的key是UIKeyboardFrameEndUserInfoKey ,从字典获取的数据类型是NSValue,我们需要通过CGRectValue方法转换成CGRect结构体,然后根据keyboard的frame来调整textView的高度。

    具体的代码附件下载。

猜你喜欢

转载自cht005288201307234627.iteye.com/blog/1929354