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

时间:2014-02-26 15:13:33

标签: ios objective-c cordova ios7

我正在iOS7上运行一个phonegap应用程序,我需要在键盘打开时以及设备方向发生变化时调整UIView的大小。

我有一些调整大小工作,但当设备的方向是LandscapeRight时,我被卡住了,第一次键盘打开时它会向上滑动,然后第二次将它向上滑动更多。

我在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)];
}

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

/*
 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];
    }
}

0 个答案:

没有答案