2016-10-17 12 views
0

キーボードが表示されてキーボードがテキストフィールドをブロックしないようにするには、次の方法を使用してビューを上に移動しています。基本的には、スクロールビューにテキストフィールドを埋め込み、キーボードが表示されたら上にスクロールします。テキストフィールドは、ビューの上部にすでにあり、それがジャンプアップするために必要がない場合IOS:キーボードが表示されているときにTextFieldを上に移動

// 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, _activeField.frame.origin)) { 
     [_scrollView scrollRectToVisible:_activeField.frame animated:YES]; 
    } 
} 

しかし、上記のコードは、それが視野の外にジャンプアップさせます。

私はそれを防ぐために追加できる行があることを思い出していますが、私はそれを覚えていません。基本的には、テキストフィールドが既に十分に高くキーボードがそれをカバーしないので、ビューを移動する必要がないかどうかをテストします。

上記のコードのif文は、テキストフィールドが非表示になっていない場合を除外しているようには見えません。

誰でもこれを行う方法を提案できますか?

ありがとうございます。

+0

、それがためにはるかに簡単になります以下の通りです通常のビューでは、ちょうどto bottom制約を調整してからlayoutIfNeededを呼び出すと、サブビューはビューから飛び出さないでしょう。 – Tj3n

+0

このことを自伝するIQkeyboardライブラリを使用することができます。あなたのプロジェクトにライブラリを置くだけで、何も実装する必要はありません。自動的に作業を開始します –

答えて

0

autolayoutを使用している場合は、制約を使用してフレームの代わりにビューを操作する必要があります。必要に応じてtextFieldの下部スペースを操作したり、スーパービューを表示したり、layoutIfNeededコールをアニメーションブロック内に置くことができます。また、下限制約の値がすでに特定の値であるかどうかを確認したり、UIKeyboardWillHideNotificationUIKeyboardWillShowNotificationにboolを渡すこともできます。そうであれば、制約の値は変更しないでください。遷移が登場するキーボードに合わせて、より思えるように私が代わりUIKeyboardDidShowNotificationUIKeyboardWillShowNotificationを使用することをお勧めします

-(void)showViewAnimatedly:(BOOL)show{ 

    if(show){ 
     [bottomConstraint setConstant:0]; 
    }else{ 
     [bottomConstraint setConstant:-160]; 
    } 

    [UIView animateWithDuration:0.3f animations:^{ 

     [self.view layoutIfNeeded]; 

    }]; 
} 

:ような何か。 UIKeyboardDidShowNotificationが移行を開始します。AFTERキーボードが表示されます。

0

「IQKeyboardManager」をチェックしてください。それはあなたの要件の優れたコントロールです。そしてそれはあなたからの1行のコードしか必要としません。これは、UITextFieldsとUITextViewsの両方で機能します。 また、前と次のボタンのオプションもあります。あなたはcocoapod

https://www.cocoacontrols.com/controls/iqkeyboardmanager

0

そのシンプルなキーボードが必要な場合は、アニメーションであなたのTextViewまたはテキストフィールドのy軸の座標を変更する表示されるプロジェクトでそれをインストールすることができます。あなたが参照できるコードは、---->あなたはscrollViewを使用し、それはサブビューの場所を制御するためにはかなり難しい場合は(おそらくサブビューのサイズといくつかの制約を調整する必要があります)

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 
+0

ここには何が表示されますか?スクロールビューにすることはできますか? 1つの画面に収まるコンテンツが多すぎるため、いずれの場合でもスクロールビューを使用しています。 – user6631314

+0

はい、uiViewのサブクラスであるものを使用できます。あなたの場合、スクロール・ビューを使用して、キーボードの呼び出しを上下にスクロールすることができます。 – vivek

+0

私は約12程度のフィールドとビューを持っています。上記は1つのテキストフィールド、つまりmailIfに基づいているようです。テキストフィールドやテキストビューに適用することは可能ですか? – user6631314

関連する問題