效果图:
// 创建评论框
_commendView = [[UIView alloc] init];
// 先放在视图最底端
_commendView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 40);
_commendView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_commendView];
_commendText = [[UITextView alloc] initWithFrame:CGRectMake(10,5, SCREEN_WIDTH - 90, 30)];
[_commendView addSubview:_commendText];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
// int width = keyboardRect.size.width;
// NSLog(@"键盘高度是 %d",height);
// NSLog(@"键盘宽度是 %d",width);
// 跟随键盘一起上移
[UIView animateWithDuration:0.4 animations:^{
_commendView.frame = CGRectMake(0, height + 12, SCREEN_WIDTH, 40);
[self.view bringSubviewToFront:_commendView];
}];
}