2016-04-06 20 views
0

キーボードが表示されたら自動的にビューを上に移動したい。すでにappleのコードhereを使用していて、うまくいきます。UIScrollView領域の可視性を自動的に移動する

これは私のオブジェクトを管理する方法です。をカバーするUIScrollViewを作成します。このUIViewは、UITextFieldUIButtonで構成されています。

Document Outline

これは、キーボードが表示されたとき、私は私の視野を調整する方法です。

#pragma mark - Keyboard Handling 

// Call this method somewhere in your view controller setup code. 
- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

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

} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

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

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your app might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, _mainView.frame.origin)) { 
     [self.scrollView scrollRectToVisible:_mainView.frame animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    _scrollView.contentInset = contentInsets; 
    _scrollView.scrollIndicatorInsets = contentInsets; 
} 

しかし、私はそれが奇妙になる点があると思います。キーボードが現れたら、スクロールしてUITextFieldが表示されます。しかし、私はそれがきつすぎると思った。私の意見で

Result

私はUITextFieldが少し上に移動すると、それが良いだろう。私の質問は、スクロールの可視性を設定するにはどうすればいいですか?私は Expectation

が少しヒントをいただければ幸いです、どうもありがとうたかっ 結果:いくつかの変数は、いくつかの

CGRect aRect = self.view.frame; 
aRect.size.height -= kbSize.height; 
if (!CGRectContainsPoint(aRect, _mainView.frame.origin)) { 
    [self.scrollView scrollRectToVisible:_mainView.frame animated:YES]; 
} 

ここに一定の注意を追加する必要があるように見えます。

答えて

0

を解決し、私はこれがによっていくつかの番号を追加することで、コンテンツのインセットを管理し解決しました。 keyboardWasShown:では、テキストフィールドとボタンの高さで内容を挿入しました。それが100であったとしましょう、そうです。

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+100, 0.0); 

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

2

最も簡単な解決策は、キーボードが開いたときにビュー(またはスクロールビュー)を上に移動することです。

- (void)keyboardWillShow:(NSNotification*)notification{ 
    [self.view setFrame:CGRectMake(0,-100, self.view.frame.size.width, self.view.frame.size.height)]; // where 100 is the offset 
    [self.view setNeedsDisplay]; 

} 

- (void)keyBoardWillHide:(NSNotification*)notification{ 
    [self.view setFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)]; 
    [self.view setNeedsDisplay]; 
} 
関連する問題