2011-01-26 11 views
3

私はUITextFieldとUITextViewを持つUIScrollViewを持っています。 キーボードが私たちが取り組んでいるフィールドを隠すことを避けたい。 ユーザーがTextFieldをタップすると、画面の上部にスクロールします。 ユーザーがTextViewをタップすると、キーボードの上部にある空の場所にリサイズします。 私のキーボードにアクセサリビュー(ツールバー)があります。iPhone - UITextFieldをスクロールし、keybordが表示されたときにUITextViewのサイズを変更する方法

今のところ、私はUIKeyboardWillShowNotification通知をキャッチし、実際には動作しません(テキストビューのサイズは変更されていますが、まだキーボードの後ろにあります)。このコードはUIScrollView 。 TextViewのためにこの作業を行う

  • - (void)keyboardWillShow:(NSNotification *)notification { 
    
        if ([self.noteView isFirstResponder] == NO) return; 
    
        /* 
        Reduce the size of the text view so that it's not obscured by the keyboard. 
        Animate the resize so that it's in sync with the appearance of the keyboard. 
        */ 
    
        NSDictionary *userInfo = [notification userInfo]; 
        NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];       // Get the origin of the keyboard when it's displayed. 
        NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; // Get the duration of the animation. 
    
        // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. 
        CGRect keyboardRect = [aValue CGRectValue]; 
        keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; 
    
        CGFloat keyboardTop = keyboardRect.origin.y; 
        CGRect newTextViewFrame = self.view.bounds; 
        newTextViewFrame.size.height = keyboardTop - self.view.frame.origin.y; // Using bounds here does not help 
    
        NSTimeInterval animationDuration; 
        [animationDurationValue getValue:&animationDuration]; 
    
        // Animate the resize of the text view's frame in sync with the keyboard's appearance. 
        [UIView beginAnimations:nil context:NULL]; 
        [UIView setAnimationDuration:animationDuration]; 
    
        self.noteView.frame = newTextViewFrame; 
    
        [UIView commitAnimations]; 
    } 
    

    は、だからあなたは私を助けることができますか?

  • Textviewを元の位置に戻しますか?

  • リサイズ機能なしでTextFieldをスクロールするにはどうすればよいですか?

答えて

0

あなただけのプログラムで、この使用UIScrollViewのスクロールでしたありがとう:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated 

ない、これはあなたのアプリケーションのために仕事をしたりしませんかどうかわからを。

+0

これは動作しません。不思議なことに、アプリは、キーボードの後ろにあるものが見えると考えているようです...おそらくkeyboardWILLshow – Oliver

関連する問題