iOS 7 iPad方向问题,当在UITextField上出现键盘后,视图向上移动时
在我的应用程序中,我不想移动/使我的ViewController的视图在UITextField上出现键盘之后向上移动。
这必须支持所有的方向.ie:
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight。
对于UIInterfaceOrientationPortrait我的代码工作正常,如下所示:
但是对于UIInterfaceOrientationPortraitUpsideDown和UIInterfaceOrientationLandscapeLeft,它不起作用 。 请看下面的截图:
UIInterfaceOrientationLandscapeLeft视图移向右侧而不是向上移动 。
对于UIInterfaceOrientationPortraitUpsideDown视图向下移动而不是向上移动 。
这只是iOS 7中的问题。我已经在iOS 8中检查过了,它工作正常。
我用下面的代码在键盘外观上animationUIView:
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 264; static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352; -(void) textFieldDidBeginEditing:(UITextField *)textField { CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; CGFloat heightFraction = numerator / denominator; if (heightFraction < 0.0) { heightFraction = 0.0; } else if (heightFraction > 1.0) { heightFraction = 1.0; } UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); } else { animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); } if(self.view.frame.origin.y != 0.000000) { self.view.frame = CGRectMake(self.view.frame.origin.x,0.0,self.view.frame.size.width,self.view.frame.size.height); } CGRect viewFrame = self.view.frame; viewFrame.origin.y -= animatedDistance; NSLog(@"View frame y pos did start: %f ",animatedDistance); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } -(void) textFieldDidEndEditing:(UITextField *)textField { CGRect viewFrame = self.view.frame; viewFrame.origin.y += animatedDistance; NSLog(@"View frame y pos did end: %f ",animatedDistance); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; }
谢谢
咕嘟咕嘟。 我能提出一些build议吗?
首先,我将附加事件到UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
而不是使用textFieldDidBeginEditing事件。
假设我在iPad上运行了您的应用程序,但连接了蓝牙键盘。 我会在文本框中点击,你的UIView
移,但iPad不会显示屏幕键盘,因为它知道我不需要它。
另一个好处是你目前使用屏幕键盘的硬编码高度。 哦,这不是一个好主意。
看看我的截图在这篇文章给一个(只有一个!)为什么这不是一个好主意的例子:
iPad键盘高度
如果您select沿着UIKeyboardWillShowNotification
路线UIKeyboardWillShowNotification
,您可以测量键盘的正确高度…
-(void)onKeyboardWillShow:(NSNotification*)notification { // The user has turned on the onscreen keyboard. // NSDictionary *info = [notification userInfo]; NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardFrame = [kbFrame CGRectValue]; keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil]; CGFloat keyboardHeight = keyboardFrame.size.height; }
这里有一个我敲的示例XCode项目,演示了如何调整控件大小,使其(几乎)填充整个屏幕,并在设备的方向和屏幕键盘可见性发生变化时resize:
OnScreenKeyboardTest XCode 6.1项目
你会看到它有一个“解除”button,这使屏幕键盘消失。 这个代码(应该)运行在所有的iPhone和iPad上。
唷! 希望这可以帮助。