简体   繁体   中英

Adjusting view frame for iOS hardware keyboard

I followed the instructions here to adjust my view with the iOS keyboard. https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

This doesn't work with a hardware keyboard. When a text view is active the iOS keyboard is not shown but the example code still returns the full height of the keyboard. In my case just the input accessory view is shown on the screen.

How do I detect this case and adjust my view for only the input accessory view?

您可以将键盘的框架与当前窗口相交,如我的答案在这里https://stackoverflow.com/a/36553555/1049134

Came across the same issue. Looks like the iOS keyboard is completely instantiated and just moved out of the view partial when a hardware keyboard is attached. Therefore the size of the keyboard is right. It is just not completely shown.

After examining the notifications I solved it with calculating the visible keyboard height myself. In my example I am listening to UIKeyboardWillShowNotification, UIKeyboardWillChangeFrameNotification and UIKeyboardWillHideNotification.

-(void)keyboardMessage:(NSNotification*)notification {
        NSDictionary *userInfo = notification.userInfo;
        CGFloat duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
        NSValue *value = userInfo[@"UIKeyboardFrameEndUserInfoKey"];
        CGRect frame = [value CGRectValue];
        [UIView animateWithDuration:duration animations:^{
            self.lowerContraint.constant = self.view.frame.size.height - frame.origin.y;;
            [self.view needsUpdateConstraints];
            [self.view layoutIfNeeded];
        }];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM