1

これをさまざまなフレーバーで実装したので、編集が開始されUITextFieldてキーボードが表示された場合、テキストフィールドを上にスクロールして表示したままにする推奨または自動化された方法はありますか? ルートビュー全体を上にスクロールするのが最も簡単で最善だと思います。私がこれまで見逃していた API に、このコードを自分で書かなくても済むようなものはありますか?

4

3 に答える 3

1

すべての UITextFields を contentView に配置し (この例では、このビューを「movableView」と呼んでいます)、ユーザーがテキスト フィールドの 1 つをタップすると、

//The hardcoded 10's and 20's are the origin of the view before
//the user starts messing with it! 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    [self scrollViewToTextField:textField];
    //Other stuff I want to do here
    return YES;
}

- (void)scrollViewToTextField:(id)textField
{
    UITextField* tf = (UITextField*)textField;

    CGPoint newOffset = tf.frame.origin;
    newOffset.x = 10;
    newOffset.y = 20 - newOffset.y;

    //This is a category method on UIView which simply adjusts the views
    //frame over a delay.
    [self.movableView moveToX:newOffset.x andY:newOffset.y withDuration:0.3f];
}

編集が終了したら、ビューを元に戻す必要があります

-(void)textFieldDidEndEditing:(UITextField *)textField {

    [self resetView];
    // do other stuff here such as grab the text and stick it in ivars etc.

}

-(void)resetView {
    [self.movableView moveToX:10.0f andY:10.0f withDuration:0.3f];
}

念のため-完全を期すためのカテゴリメソッドを次に示します

//  UIView+BasicAnimation.h
-(void) moveToX:(CGFloat) x andY:(CGFloat) y withDuration:(NSTimeInterval) duration;


//  UIView+BasicAnimation.m
-(void) moveToX:(CGFloat) x andY:(CGFloat) y withDuration:(NSTimeInterval) duration {
    CGRect newFrame = self.frame;
    newFrame.origin.x = x;
    newFrame.origin.y = y;

    [UIView beginAnimations:@"BasicAnimation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:duration];

    self.frame = newFrame;

    [UIView commitAnimations];
}
于 2012-06-14T15:09:29.097 に答える
0

UIScrollViewフィールドがfirstResponderステータスを取得したときに、を使用してコンテンツオフセットを変更することにより、これを実装するのが一般的です。

于 2012-06-14T14:07:58.123 に答える
0

そのようなものは存在しないと思いますし、奇妙な省略のように思えます。iPad の分割キーボードの存在を考えると、一度正しく行われ、API で提供されるべきもののように思えます。

現在のプロジェクトでは、 を試していTPKeyboardAvoidingScrollViewます。(https://github.com/michaeltyson/TPKeyboardAvoiding)

于 2012-06-14T14:04:28.330 に答える