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];
}