2012-03-10 99 views
1

对于我的英语语言感到抱歉。键盘出现时移动UIView

我试图找到它的行为之前。但问题是ViewController在景观和创建UIView一半的ViewController。在UIViewUITextView。但现在当键盘出现在ViewController的背景下时,在键盘下方向下滚动。并只看到UIView。如果触摸空间,键盘将消失,背景回归。我想只是在键盘出现时移动UIView

非常感谢。

回答

10

试试这个

- (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]; 

    } 
} 
+2

你应该在你的 'viewDidAppear' 添加super调用。 – jack 2013-10-07 14:26:07

1

This answer看起来可能是你要找的东西。

总之:当键盘出现与UIKeyboardDidShowNotification

  1. 检测。

  2. 该通知的user info描述了键盘的框架。

  3. 调整视图的框架以使其从键盘下方出来。

相关问题