您的位置:首页 > 其它

中/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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: