2016-04-23 6 views
1

一般的なメッセージングアプリケーションのようなテキストビューとボタンが付いたビューがあります。キーボードが表示されると、テキストビューとボタンも上方向に押し込まれますが、コンテンツは上端が表示されない画面から上に押し出されますが、強制的に上にスクロールすると上に上がります私が離れるとき。私は素早いスケッチで問題を説明しました。第3の画面は望ましい動作です。ソフトウェアキーボードを押したときのUIScrollViewのコンテンツの高さを変更する方法

このビューは.xibファイルにあります。私は、ビューを持っている - 私は、キーボードの通知を登録し、ここで表示/非表示方法

- (void)registerForKeyboardNotifications { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWasShown:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillBeHidden:) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 

} 

- (void)keyboardWasShown:(NSNotification *) aNotification { 
    NSDictionary *info = [aNotification userInfo]; 

    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

    // the hardcoded 49 is the height of the uitabbar 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    [self.view addGestureRecognizer:self.tapRecognizer]; 

} 


- (void)keyboardWillBeHidden:(NSNotification *) aNotification { 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    [self.view removeGestureRecognizer:self.tapRecognizer]; 
} 
ためのコードです(テーブルとのTextViewとボタンがあります)> contentView

enter image description here

- > ScrollView

答えて

0

これを試してください。

// Called when the UIKeyboardWillShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification{ 

    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

    CGPoint scrollPoint = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom); 
    [scrollView setContentOffset:scrollPoint animated:true]; 

} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 
+0

それは残念ながら私のために働かなかった。テキストビューとボタンはスクロールビューで移動しています。 – ioskaveen

+0

編集した答えを試してください –

+0

それはどちらも動作しません。私がしようとしているのは、ソフトウェアキーボードとナビゲーションバーのすぐ下にあるビューの一番上にあるスクロールビューを合わせることです – ioskaveen

1

私はkeyboardWasShown方法でこのラインを見ているあなたのトップエッジのインセットが-veある

UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0); 

、49 becouse - (APPX 256である)keyboardSize。これに置き換えて:

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height-49), 0.0); 

あなたに最も適したスイートを演出します。この作品を期待してください:)

関連する問題