0

UITextField誰かがそれを編集しようとしてキーボードがポップアップすると、ユーザーが入力内容を確認できるようにビューが上に移動します。しかし、キーボードを閉じると、ビューが元の位置に戻りません! プロパティを使用しCGPointて元の位置をキャプチャしviewDidLoad、キーボードを閉じたときに元の位置に戻そうとしています。

コード:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Set the original center point for shifting the view
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    self.originalCenter = self.view.center;
}

- (void)doneWithNumberPad {

    [locationRadius resignFirstResponder];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = self.originalCenter;
    [UIView commitAnimations];
}

- (void)keyboardDidShow:(NSNotification *)note
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = CGPointMake(self.originalCenter.x, 150);
    [UIView commitAnimations];
}
4

2 に答える 2

1

When viewDidLoad is called, your view hierarchy has not yet been laid out for the current device's screen size or orientation. It is too early to look at self.view.center.

Do it in viewDidLayoutSubviews instead.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.originalCenter = self.view.center;
}

Note that if you support autorotation, even this won't work properly if the keyboard is visible when the autorotation happens.

于 2013-02-22T21:33:46.397 に答える
0

絶対的な最終的な中心位置を必要としない場合、これを機能させるための確実な方法は、キーボードが表示されたときに定義した固定値だけビューを上にシフトし、キーボードが非表示になったときにその固定値だけビューを下にシフトすることです.

#define OFFSET 100

- (void)doneWithNumberPad {
    [locationRadius resignFirstResponder];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y + OFFSET);
    [UIView commitAnimations];
}

- (void)keyboardDidShow:(NSNotification *)note
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y - OFFSET);
    [UIView commitAnimations];
}
于 2013-02-22T23:23:11.163 に答える