UIKeyboardWillShowNotification

UIKeyboardWillShowNotification

本文介绍了iOS 7 iPad方向问题,当键盘出现在UITextField上时视图向上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,在UITextField上出现键盘后,我不想将ViewController的视图移动/动画向上方向。

In my application i wan't to move/animate my ViewController's view to upward direction after appearance of keyboard over UITextField.

这必须支持所有方向。即:

UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight。

and this must support for all orientation.i.e:
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight.

对于UIInterfaceOrientationPortrait,我的代码工作正常,如图所示下面:

但对于UIInterfaceOrientationPortraitUpsideDown和UIInterfaceOrientationLandscapeLeft,它无法正常工作。请看下面的截图:

But for UIInterfaceOrientationPortraitUpsideDown and UIInterfaceOrientationLandscapeLeft it is not working. please see below screenshots:

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

谢谢

推荐答案

Gulp。我可以提出一些建议吗?

Gulp. Can I make some suggestions ?

首先,我会将一个事件附加到 UIKeyboardWillShowNotification UIKeyboardWillHideNotification 而不是使用textFieldDidBeginEditing事件。

First, I would attach an event to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification rather than using the textFieldDidBeginEditing event.

假设我在iPad上运行了你的应用程序,但连接了蓝牙键盘。我会点击文本框,你的 UIView 会向上移动,但iPad不会显示屏幕键盘,因为它知道我不需要它。

Supposing I ran your app on my iPad, but had a Bluetooth keyboard connected. I would tap in the text box, your UIView would shift up, but the iPad wouldn't display an onscreen keyboard as it knows I wouldn't need it.

另一个优点是您目前正在使用屏幕键盘的硬编码高度。哦,这不是一个好主意。

The other advantage is that you're currently using hardcoded heights for the onscreen keyboard. Ooooh, this isn't a good idea.

在这篇文章中查看我的截图给出一个(只有一个!)示例,说明为什么这不是一个好主意:

Have a look at my screenshot in this posting to give one (just one!) example of why this isn't a good idea:

如果您选择关闭 UIKeyboardWillShowNotification 路线,你可以测量键盘的正确高度...

If you choose to go down the UIKeyboardWillShowNotification route, you can measure the correct height of the keyboard...

-(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项目,它演示了如何调整大小一个控件,使其(几乎)填满整个屏幕,并在设备的方向和尺寸调整时调整大小。屏幕键盘可见性更改:

Here's a sample XCode project I've knocked up, which demonstrates how to resize a control, to make it (almost) fill the entire screen, and resize it when the device's orientation & onscreen keyboard visibility changes:

你会看到它有一个Dismiss按钮,这会让屏幕键盘消失。这段代码(应该)可以在所有iPhone上运行。 iPads。

You'll see that it has a "Dismiss" button, which makes the onscreen keyboard go away. This code (should) run on all iPhones & iPads.

P!
希望这有帮助。

Phew !Hope this helps.

这篇关于iOS 7 iPad方向问题,当键盘出现在UITextField上时视图向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:10