我有一个仅以纵向和纵向向上运行的应用程序。我需要在键盘出现时将视图向上推,并在它消失时将其拉回。如果设备保持纵向,则以下代码可以完美运行,但如果它是纵向的,则视图会移动错误的方向(-260 而不是 260),而且如果在显示键盘时方向发生变化,则不会处理...... keyboardWillHide 方法在两个方向都可以正常工作。有没有办法将视图相对移动到键盘或状态栏,所以设备的方向无关紧要???
- (void) keyboardWillShow:(NSNotification *) notification
{
NSLog(@"Keyboard Will Show");
double animationDuration;
animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + -260);
}completion:^(BOOL finished){
}];
}
- (void) keyboardWillHide:(NSNotification *) notification
{
double animationDuration;
animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSLog(@"Keyboard Will Hide");
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}completion:^(BOOL finished){
}];
}