键盘出现时移动UIView
问题描述:
对于我的英语语言感到抱歉。键盘出现时移动UIView
我试图找到它的行为之前。但问题是ViewController
在景观和创建UIView
一半的ViewController
。在UIView
有UITextView
。但现在当键盘出现在ViewController
的背景下时,在键盘下方向下滚动。并只看到UIView
。如果触摸空间,键盘将消失,背景回归。我想只是在键盘出现时移动UIView
。
非常感谢。
答
试试这个
- (void)viewDidAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note
{
CGRect keyboardBounds;
NSValue *aValue = [note.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
[aValue getValue:&keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
if (!keyboardIsShowing)
{
keyboardIsShowing = YES;
CGRect frame = view.frame;
frame.size.height -= 168;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
view.frame = frame;
[UIView commitAnimations];
}
}
- (void)keyboardWillHide:(NSNotification *)note
{
CGRect keyboardBounds;
NSValue *aValue = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
[aValue getValue: &keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
if (keyboardIsShowing)
{
keyboardIsShowing = NO;
CGRect frame = view.frame;
frame.size.height += 168;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
view.frame = frame;
[UIView commitAnimations];
}
}
答
This answer看起来可能是你要找的东西。
总之:当键盘出现与UIKeyboardDidShowNotification
检测。
该通知的user info描述了键盘的框架。
调整视图的框架以使其从键盘下方出来。
你应该在你的 'viewDidAppear' 添加super调用。 – jack 2013-10-07 14:26:07