在我的应用程序中,我不想在UITextField上出现键盘后将ViewController的视图移动/动画到向上方向。
这必须支持所有orientation.i.e:
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight。
对于UIInterfaceOrientationPortrait,我的代码工作正常,如下所示:
但对于UIInterfaceOrientationPortraitUpsideDown和UIInterfaceOrientationLandscapeLeft,它无法正常工作。请看下面的截图:
对于UIInterfaceOrientationLandscapeLeft视图向右侧移动而不是向上侧。
对于UIInterfaceOrientationPortraitUpsideDown视图向下移动而不是向上移动。
这只是iOS 7中的问题。我已经在iOS 8中检查了它,它运行正常。
我使用下面的代码在键盘外观上设置动画UIView:
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];
}
由于
答案 0 :(得分:1)
杯。我可以提出一些建议吗?
首先,我会将事件附加到UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
,而不是使用textFieldDidBeginEditing事件。
假设我在iPad上运行了你的应用程序,但连接了蓝牙键盘。我会点击文本框,你的UIView
会向上移动,但iPad不会显示屏幕键盘,因为它知道我不需要它。
另一个优点是您目前正在使用屏幕键盘的硬编码高度。哦,这不是一个好主意。
在这篇文章中查看我的屏幕截图,给出一个(只有一个!)示例,说明为什么这不是一个好主意:
如果您选择沿着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项目,我已经敲了,它演示了如何调整控件的大小,使其(几乎)填满整个屏幕,并在设备的方向和放大时调整大小;屏幕键盘可见性更改:
OnScreenKeyboardTest XCode 6.1 project
你会发现它有一个&#34; Dismiss&#34;按钮,使屏幕键盘消失。这段代码(应该)可以在所有iPhone上运行。 ipad公司
P! 希望这会有所帮助。