在iPhone的短信对话界面中,当点击下方的输入框时,整个信息列表界面的大小会自动发生改变并保证界面内容不被键盘所覆盖.要实现这种效果需要在键盘将要弹出时使用代码动态的改变整个View的大小.
首先在viewDidLoad中注册键盘弹出事件监听器:
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];
在键盘弹出时,获得键盘高度,动态改变View大小:
-(void) keyboardWillShow:(NSNotification *)note
{
NSLog(@"keyboardWillShow %d",keyboardIsShowing);
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
NSLog(@"keyboardHeight = %d",keyboardHeight);
if (keyboardIsShowing == NO)
{
keyboardIsShowing = YES;
NSLog(@"change %d %f",keyboardIsShowing,self.view.frame.size.height);
CGRect frame = self.view.frame;
frame.size.height -= keyboardHeight;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = frame;
[UIView commitAnimations];
}
}
需要注意的是在界面退出时务必removeObserver.
转载请注明:来自ZhaoZhe's Blog
本文地址:http://zhao-zhe.appspot.com/?p=138001