关于键盘弹出时遮盖页面显示的解决方案

来源:互联网 发布:国学宝典数据库 编辑:程序博客网 时间:2024/05/19 03:17

在看asihttprequest代码时候,无意发现了关于键盘弹出时遮盖页面显示的解决方案,

解决方法如下:

在viewload方法里面注册监听键盘弹出和hide 

[[selfview] setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];


然后在keyboardWillShow修改页面的大小- 键盘的高度

- (void)keyboardWillShow:(NSNotification *)notification

{

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2

NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];

#else

NSValue *keyboardBoundsValue = [[notificationuserInfo] objectForKey:UIKeyboardBoundsUserInfoKey];

#endif

CGRect keyboardBounds;

[keyboardBoundsValue getValue:&keyboardBounds];

UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height-42,0);

[[selftableView] setScrollIndicatorInsets:e];

[[selftableView] setContentInset:e];

}

恢复正常页面

- (void)keyboardWillHide:(NSNotification *)notification

{

UIEdgeInsets e =UIEdgeInsetsMake(0,0, 0,0);

[[selftableView] setScrollIndicatorInsets:e];

[[selftableView] setContentInset:e];

}