2012-03-10 17 views
1

英語の言語については残念です。キーボードが表示されているときにUIViewを移動

私はそれが動作する前に見つけようとしました。しかし問題は、風景の中のViewControllerであり、の半分をUIViewと作成しました。 UIViewにはUITextViewがあります。しかし、今キーボードがキーボードの下にスクロールViewControllerの背景が表示されます。ちょうどUIViewを参照してください。タッチスペースがあれば、キーボードは消え、バックグラウンドが復活します。私はちょうどキーボードが表示されたらUIViewを移動したいです。

ありがとうございました。

答えて

10

あなたはあなたの 'viewDidAppear' でスーパーの呼び出しを追加する必要があります。この

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

を試してみてください。 – jack

1

This answerあなたが探しているかもしれないようです。要するに

:キーボードはUIKeyboardDidShowNotificationと表示されたときに

  1. を検出します。

  2. user infoには、キーボードのフレームが記載されています。

  3. ビューのフレームを調整して、キーボードの下から外に出すようにします。

関連する問題