我有一个简单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];
}