纵向的高度和横向的高度以点为单位。

最佳答案

我使用以下方法确定iOS 7.1中的键盘框架。

在我的 View Controller 的init方法中,我注册了UIKeyboardDidShowNotification:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];

然后,我在keyboardOnScreen:中使用以下代码来访问键盘框架。此代码从通知中获取userInfo字典,然后访问与NSValue关联的UIKeyboardFrameEndUserInfoKey。然后,您可以访问CGRect并将其转换为 View Controller 的 View 坐标。从那里,您可以基于该框架执行所需的任何计算。
-(void)keyboardOnScreen:(NSNotification *)notification
 {
        NSDictionary *info  = notification.userInfo;
        NSValue      *value = info[UIKeyboardFrameEndUserInfoKey];

        CGRect rawFrame      = [value CGRectValue];
        CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];

        NSLog(@"keyboardFrame: %@", NSStringFromCGRect(keyboardFrame));
 }

迅捷

以及Swift的等效实现:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)


@objc
func keyboardDidShow(notification: Notification) {
    guard let info = notification.userInfo else { return }
    guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
    let keyboardFrame = frameInfo.cgRectValue
    print("keyboardFrame: \(keyboardFrame)")
}

关于ios - iPhone屏幕键盘的高度是多少?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11284321/

10-12 20:00