iOS 8 Orientation change: Keyboard frame does not display correctly

后端 未结 2 687
遥遥无期
遥遥无期 2021-01-13 05:24

This is only an iOS 8 problem, the keyboard displays correctly in iOS 7 with device orientation change. My application supports both portrait and landscape orientation and u

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-13 06:05

    Prior to iOS 8, the keyboard's location and width/height were always relative to portrait orientation when reported to the app. (e.g. Landscape's keyboard width is in the y direction, ~352 pixels on an iPad.) As of iOS 8, this has been updated to always have (0,0) at the top left of your (physical) view and the width/height reflect the x/y orientation you would normally expect outside of iOS. If you were previously positioning your keyboard via something like keyboardDidShow's [notification userInfo], you are going to get numbers that don't quite make sense. You can use something along these lines to take into account the pre-iOS8 idiosyncrasies:

    - (void)keyboardDidShow: (NSNotification *) notification{
    
        NSDictionary *keyboardInfo = [notification userInfo];
        CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        float height, width;
        if(UIInterfaceOrientationIsPortrait(orientation)){
            width = keyboardSize.width;
            height = keyboardSize.height;
        } else {
            if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1){
                width = keyboardSize.height;
                height = keyboardSize.width;
            } else {
                width = keyboardSize.width;
                height = keyboardSize.height;
            }
        }
    
        // Remainder of function
    }
    

    Which can be refactored down to...

    - (void)keyboardDidShow: (NSNotification *) notification{
    
        NSDictionary *keyboardInfo = [notification userInfo];
        CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        float width = keyboardSize.width;
        float height = keyboardSize.height;
        if(!UIInterfaceOrientationIsPortrait(orientation) && (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1)){
            width = keyboardSize.height;
            height = keyboardSize.width;
        }
    
        // Remainder of function
    }
    

    Also, the 8.1 update fixed several landscape/rotation bugs likely related to the above change. Grab the update and see if that solves your issue.

提交回复
热议问题