Ios 当显示键盘时,iPad调整UIView的大小

Ios 当显示键盘时,iPad调整UIView的大小,ios,objective-c,cordova,ios7,Ios,Objective C,Cordova,Ios7,我正在iOS7上运行phonegap应用程序,需要在键盘打开和设备方向改变时调整UIView的大小 我有一些调整大小的工作,但我卡住了当设备的方向是景观权利,第一次键盘打开它向上滑动,然后第二次它向上滑动,甚至更多 我在stackoverflow上搜索过类似的答案,但没有一个解决了我要寻找的问题。所以,请不要只是从其他地方复制和粘贴解决方案,谢谢 MainViewController.m int screenWidth; int screenHeight; float keyboard_of

我正在iOS7上运行phonegap应用程序,需要在键盘打开和设备方向改变时调整UIView的大小

我有一些调整大小的工作,但我卡住了当设备的方向是景观权利,第一次键盘打开它向上滑动,然后第二次它向上滑动,甚至更多

我在stackoverflow上搜索过类似的答案,但没有一个解决了我要寻找的问题。所以,请不要只是从其他地方复制和粘贴解决方案,谢谢

MainViewController.m

int screenWidth;

int screenHeight;

float keyboard_offset = 80.0;

- (void)viewWillAppear:(BOOL)animated
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];

        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 20;
        viewBounds.size.height = viewBounds.size.height - 20;
        self.webView.frame = viewBounds;

        self.webView.backgroundColor = [UIColor blackColor];
        self.webView.opaque=NO;
        screenWidth = self.view.frame.size.width;
        screenHeight = self.view.frame.size.height;
    }

    [super viewWillAppear:animated];
}

- (void)orientationChange:(NSNotification *)note
{
    UIDevice * device = note.object;

    switch(device.orientation)
    {
        case UIDeviceOrientationLandscapeLeft:
            [self.view setFrame: CGRectMake(-keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
            break;
        case UIDeviceOrientationLandscapeRight:
            [self.view setFrame: CGRectMake(keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
            break;
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
            [self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
            break;
        default:
            break;
    };
}

- (void)keyboardDidShow:(NSNotification *) notification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:)
                                          name:UIDeviceOrientationDidChangeNotification
                                          object:[UIDevice currentDevice]];

    if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft)
    {
        [self.view setFrame: CGRectMake(-keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
    }
    else if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
    {
        [self.view setFrame: CGRectMake(keyboard_offset, 0, screenWidth + keyboard_offset,  screenHeight)];
    }
    else
    {
        [self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
    }
}

- (void)keyboardDidHide:(NSNotification *) notification
{
    [self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
}
/*
 Change the status bar color to the old style
 */
-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    /*
     Only perform these actions for iOS 7 or above
     */
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        [self setNeedsStatusBarAppearanceUpdate];
    }
}
AppDelegate.m

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{   
    /*
     Orientation change for iOS7
    */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {

        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                                name:UIDeviceOrientationDidChangeNotification
                                                   object:[UIDevice currentDevice]];

        self.window.clipsToBounds = YES;
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
    }

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

/**
 * Change the status bar color on orientation change.
 */
- (void) orientationChanged:(NSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationLandscapeLeft:
            self.window.frame =  CGRectMake(-20, 0, screenWidth, screenHeight);
            self.window.bounds = CGRectMake(-20, 0, screenWidth, screenHeight);
        break;
        case UIDeviceOrientationLandscapeRight:
            self.window.frame =  CGRectMake(20, 0, screenWidth, screenHeight);
            self.window.bounds = CGRectMake(20, 0, screenWidth, screenHeight);
        break;
        case UIDeviceOrientationPortrait:
            self.window.frame =  CGRectMake(0, 20, screenWidth, screenHeight);
            self.window.bounds = CGRectMake(0, 20, screenWidth, screenHeight);
        break;
        case UIDeviceOrientationPortraitUpsideDown:
            self.window.frame =  CGRectMake(0, -20, screenWidth, screenHeight);
            self.window.bounds = CGRectMake(0, -20, screenWidth, screenHeight);
        break;
        default:
        break;
    };
}
CDVViewController.m

int screenWidth;

int screenHeight;

float keyboard_offset = 80.0;

- (void)viewWillAppear:(BOOL)animated
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];

        CGRect viewBounds = [self.webView bounds];
        viewBounds.origin.y = 20;
        viewBounds.size.height = viewBounds.size.height - 20;
        self.webView.frame = viewBounds;

        self.webView.backgroundColor = [UIColor blackColor];
        self.webView.opaque=NO;
        screenWidth = self.view.frame.size.width;
        screenHeight = self.view.frame.size.height;
    }

    [super viewWillAppear:animated];
}

- (void)orientationChange:(NSNotification *)note
{
    UIDevice * device = note.object;

    switch(device.orientation)
    {
        case UIDeviceOrientationLandscapeLeft:
            [self.view setFrame: CGRectMake(-keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
            break;
        case UIDeviceOrientationLandscapeRight:
            [self.view setFrame: CGRectMake(keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
            break;
        case UIDeviceOrientationPortrait:
        case UIDeviceOrientationPortraitUpsideDown:
            [self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
            break;
        default:
            break;
    };
}

- (void)keyboardDidShow:(NSNotification *) notification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange:)
                                          name:UIDeviceOrientationDidChangeNotification
                                          object:[UIDevice currentDevice]];

    if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft)
    {
        [self.view setFrame: CGRectMake(-keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
    }
    else if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
    {
        [self.view setFrame: CGRectMake(keyboard_offset, 0, screenWidth + keyboard_offset,  screenHeight)];
    }
    else
    {
        [self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
    }
}

- (void)keyboardDidHide:(NSNotification *) notification
{
    [self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
}
/*
 Change the status bar color to the old style
 */
-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    /*
     Only perform these actions for iOS 7 or above
     */
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        [self setNeedsStatusBarAppearanceUpdate];
    }
}

可以附加一个屏幕截图吗?第三次比第二次更高,对吗?@eli.d第一次按预期工作,然后第二次等等会更高,但每次键盘打开时(如果有意义的话)不会更高有什么建议@elio.d