1

15個のテキストフィールドを持つアプリを開発しています。そこで、UIScrollViewを使用してスクロールを可能にしました。しかし、私の最後の5つのテキストフィールドは、編集するためにそれらのテキストフィールドをクリックすると、キーボードの後ろに隠れます。編集モードになったら、これらのテキストフィールドをキーボードの上に移動するにはどうすればよいですか?繰り返しますが、テキストフィールドはUIScrollViewにあります。UIViewではありません。

4

4 に答える 4

2

TPKeyboardAvoidingScrollViewを試してください。

既に持っているスクロール ビューを取得し、そのクラスを に変更するだけですTPKeyboardAvoidingScrollView。キーボードがポップアップすると、フィールドが適切に表示されるようにサイズとオフセットが調整されます。

複雑なレイアウト (多くのテキスト フィールドやその他のコンポーネント) がある場合、手動でこれを行うのは難しい場合があるため、これをお勧めします。このコントロールはとてもシンプルで、魅力的に機能します!

于 2012-10-03T17:10:34.737 に答える
1

サンプルコードは次のとおりです。

#define kOFFSET_FOR_KEYBOARD 80.0

-(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
{
    // 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
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}
于 2012-10-03T17:56:07.960 に答える
0

scrollView に応じて contentSize と contentOffset を設定する必要があります。したがって、最後のテキスト ボックスが (0,300) の原点にある場合、おそらく contentSize を CGSizeMake(0, 600) 程度にしてから、contentOffset を (0, 250) に設定する必要があります。

于 2012-10-03T17:06:00.473 に答える
0

scrollView Y座標を減らすことができますtextFieldShouldBeginEditing:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   if(textField == textField11 || textField == textField12 || textField == textField13 || textField == textField14 || textField == textField15)
   {   
       [UIView beginAnimations:nil context:NULL];
       [UIView setAnimationDuration:0.3];
       //set Y according to keyBoard height
       [scrollView setFrame:CGRectMake(0.0,-220.0,320.0,460.0)];
       [UIView commitAnimations];
   }
}

キーボードのリターンキーscrollViewを押したときのようにフレームを設定します

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [scrollView setFrame:CGRectMake(0.0,0.0,320.0,460.0)];
    [UIView commitAnimations];   
}
于 2012-10-03T17:19:53.813 に答える