1

ネイティブiOSMail.appには、新しい文字を作成するときに優れた機能があります。画面全体がaUIScrollViewで、文字の本文を書き込む場所はUITextViewスクロールが無効になっている場所です。

ここに画像の説明を入力してください

これTextViewの高さとUIScrollView変更の高さを動的に入力すると、下にUIScrollViewスクロールして、新しいテキスト用にキーボードの上にいくつかのピクセルを残します。

このプロセスはtextViewDidChangeメソッドで実行する必要があることはわかっていますが、同じことを実行しようとすると、コードで問題が発生するUITextField可能性がありUIScrollViewます。これを行う方法は次のとおりです。

-(void)textViewDidChange:(UITextView *)textView {

    CGRect frame = emailTextView.frame;
    frame.size.height = emailTextView.contentSize.height;
    emailTextView.frame = frame;
    mainScrollView.contentSize = CGSizeMake(320, emailTextView.contentSize.height + rightKeyboardSize.height + 20);

}

ここで何がうまくいかないかについてのアイデアはありますか?前もって感謝します!

4

1 に答える 1

1

わかりました。答えを見つけるために、独自のサンプルプロジェクトを実装しました。自分の名前を置き換える必要があります。

まず、キーボードが表示または非表示になったことを検出するオブザーバーを追加します。

- (void)addKeyboardObserver
{
    // This could be in an init method.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardDidShow:(NSNotification*)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    _keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    UIScrollView *_scrollView = (UIScrollView*)self.view;
    _scrollView.frame = CGRectMake(_scrollView.frame.origin.x,
                                   _scrollView.frame.origin.y,
                                   _scrollView.frame.size.width,
                                   _scrollView.frame.size.height - _keyboardFrameBeginRect.size.height);
}

- (void)keyboardDidHide:(NSNotification*)notification
{
    UIScrollView *_scrollView = (UIScrollView*)self.view;
    _scrollView.frame = CGRectMake(_scrollView.frame.origin.x,
                                   _scrollView.frame.origin.y,
                                   _scrollView.frame.size.width,
                                   _scrollView.frame.size.height +
                               _keyboardFrameBeginRect.size.height);
}

次に、textViewDidChange:メソッドが次のように変更されました。

- (void)textViewDidChange:(UITextView *)textView
{
    UIScrollView *_scrollView = (UIScrollView*)self.view;
    _textView.frame = CGRectMake(_textView.frame.origin.x,
                                 _textView.frame.origin.y,
                                 _textView.contentSize.width,
                                 _textView.contentSize.height);
    _scrollView.contentSize = _textView.frame.size;

    if (_scrollView.frame.size.height < _textView.frame.size.height) {
        CGPoint bottomOffset = CGPointMake(0,_textView.frame.size.height-_keyboardFrameBeginRect.size.height);
        [_scrollView setContentOffset:bottomOffset animated:NO];
    }
}

幸運を!

于 2013-01-30T12:47:05.623 に答える