中/E文键盘切换高度监测
2013-07-31 16:26
148 查看
首先:宏定义
//判断是否是iphone5
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136),
[[UIScreen mainScreen] currentMode].size) : NO)
//监听键盘的宏定义
#define _UIKeyboardFrameEndUserInfoKey (&UIKeyboardFrameEndUserInfoKey != NULL ? UIKeyboardFrameEndUserInfoKey : @"UIKeyboardBoundsUserInfoKey")
然后,viewDiload里面写监听:
//键盘的监听事件,获取高度
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification
object:nil];
最后在当前的controller里面写上如下方法:
//键盘事件
- (void)keyboardWillShow:(NSNotification*)notification{
CGRect _keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey]CGRectValue];
//如下是在此方法中去改变tabview或者其他需要调整高度的view
myTabView.frame = CGRectMake(0, 40, 320, iPhone5 ? 460+88-85-_keyboardRect.size.height :460-85-_keyboardRect.size.height);
}
记住在dealloc中
[[NSNotificationCenterdefaultCenter]
removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]
removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
否则会引起崩溃,因为当界面消失时会对nil对象sendMessage
#pragma mark -
#pragma mark Keyboard notifications from Apple's UICatalog example
//The code comes straight from Apple's UICatalog example, except that I have modified the UI manipulation
//to use bounds instead of frame, so that when keyboard is shown, the view "scrolls" to the right place
//so that the input field would remain visible.
- (void)keyboardWillShow:(NSNotification *)aNotification {
// the keyboard is showing so resize the my height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect bounds = self.view.bounds;
bounds.origin.y += keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.bounds = bounds;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification {
// the keyboard is hiding reset the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect bounds = self.view.bounds;
bounds.origin.y -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.bounds = bounds;
[UIView commitAnimations];
}
//判断是否是iphone5
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136),
[[UIScreen mainScreen] currentMode].size) : NO)
//监听键盘的宏定义
#define _UIKeyboardFrameEndUserInfoKey (&UIKeyboardFrameEndUserInfoKey != NULL ? UIKeyboardFrameEndUserInfoKey : @"UIKeyboardBoundsUserInfoKey")
然后,viewDiload里面写监听:
//键盘的监听事件,获取高度
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification
object:nil];
最后在当前的controller里面写上如下方法:
//键盘事件
- (void)keyboardWillShow:(NSNotification*)notification{
CGRect _keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey]CGRectValue];
//如下是在此方法中去改变tabview或者其他需要调整高度的view
myTabView.frame = CGRectMake(0, 40, 320, iPhone5 ? 460+88-85-_keyboardRect.size.height :460-85-_keyboardRect.size.height);
}
记住在dealloc中
[[NSNotificationCenterdefaultCenter]
removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]
removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
否则会引起崩溃,因为当界面消失时会对nil对象sendMessage
#pragma mark -
#pragma mark Keyboard notifications from Apple's UICatalog example
//The code comes straight from Apple's UICatalog example, except that I have modified the UI manipulation
//to use bounds instead of frame, so that when keyboard is shown, the view "scrolls" to the right place
//so that the input field would remain visible.
- (void)keyboardWillShow:(NSNotification *)aNotification {
// the keyboard is showing so resize the my height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect bounds = self.view.bounds;
bounds.origin.y += keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.bounds = bounds;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification {
// the keyboard is hiding reset the table's height
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect bounds = self.view.bounds;
bounds.origin.y -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.bounds = bounds;
[UIView commitAnimations];
}
相关文章推荐
- Objective-C-如何监测键盘的高度(iOS)
- 有手机输入键盘时切换横竖屏高度变化的问题
- iOS键盘中英文切换键盘高度获取通知方法
- iOS监测第三方键盘高度
- iOS键盘中英文切换键盘高度获取通知方法
- iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移
- iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移
- UITextView自适应内容的高度且实现光标总是距离键盘一段距离而滑动条却可以延伸到光盘
- iOS开发中的键盘高度变化处理
- 键盘切换中文
- thinkpad键盘切换
- [ios] 适应键盘高度变化
- ThinkPad紧凑型蓝牙键盘(0B47189)鼠标滚轮用法,F1到F12功能键的功能切换以及其他技巧
- iOS5 切换中文键盘时覆盖输入框的完美解决方案
- 完美适应键盘高度变化
- iphone键盘高度
- KeyAndMouse - 鼠标及键盘动作监测组件
- C#监测全局键盘与鼠标事件
- iOS中改变键盘的高度 监听键盘
- iOS开发之禁止用户切换键盘(防止用户切换其它键盘)