iPhone屏幕键盘的高度是多少?

时间:2012-07-01 17:49:22

标签: ios iphone keyboard height

纵向高度和横向高度。以点数衡量。

8 个答案:

答案 0 :(得分:205)

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

在我的视图控制器的init方法中,我注册了UIKeyboardDidShowNotification

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

然后,我使用keyboardOnScreen:中的以下代码来访问键盘的框架。此代码从通知中获取userInfo字典,然后访问与NSValue关联的UIKeyboardFrameEndUserInfoKey。然后,您可以访问CGRect并将其转换为视图控制器视图的坐标。从那里,您可以根据该框架执行所需的任何计算。

-(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

与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)")
}

答案 1 :(得分:46)

请记住,对于iOS 8,屏幕键盘的尺寸可以不同。不要以为屏幕键盘始终可见(具有特定的高度)或不可见。

现在,使用iOS 8,用户还可以打开和关闭文本预测区域......当他们这样做时,它会启动应用程序的keyboardWillShow事件再次

这个打破很多遗留代码示例,建议编写keyboardWillShow事件,该事件仅测量屏幕键盘的当前高度,并上下调整控件这个(绝对)金额的页面。

enter image description here

换句话说,如果您看到任何示例代码,它只是告诉您添加keyboardWillShow事件,请测量键盘高度,然后调整控件的大小。这个数量的高度,这将不再总是有效。

在上面的示例中,我使用了以下网站中的示例代码,该代码为垂直约束constant值设置了动画。

Practicing AutoLayout

在我的应用中,我为UITextView添加了一个约束,设置在屏幕底部。当屏幕第一次出现时,我存储了这个初始垂直距离。

然后,每当我的keyboardWillShow事件开始时,我将(新的)键盘高度添加到此原始约束值(因此约束调整大小控制的高度)。

enter image description here

呀。这太丑了。

我很恼火/感到惊讶的是,XCode 6令人痛苦的AutoLayout并不能让我们将控件底部连接到屏幕底部或顶部屏幕键盘。

也许我错过了什么。

除了我的理智。

答案 2 :(得分:35)

纵向模式的键盘高度为216pts,横向模式的键盘高度为162pts。

Source

答案 3 :(得分:16)

版本说明:这不再是iOS 9& 10,因为它们支持自定义键盘尺寸。

这取决于型号和QuickType栏:

enter image description here

http://www.idev101.com/code/User_Interface/sizes.html

答案 4 :(得分:10)

键盘高度取决于型号,QuickType栏,用户设置......最好的方法是以dinamically计算:

Swift 3.0

    var heightKeyboard : CGFloat?

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    }

    func keyboardShown(notification: NSNotification) {
           if let infoKey  = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
               let rawFrame = (infoKey as AnyObject).cgRectValue {
               let keyboardFrame = view.convert(rawFrame, from: nil)
               self.heightKeyboard = keyboardFrame.size.height
               // Now is stored in your heightKeyboard variable
           }
    }

答案 5 :(得分:6)

我找不到最新的答案,所以我用模拟器检查一下。(iOS 11.0)

设备|屏幕高度|肖像|景观

iPhone 4s | 480.0 | 216.0 | 162.0

iPhone 5,iPhone 5s,iPhone SE | 568.0 | 216.0 | 162.0

iPhone 6,iPhone 6s,iPhone 7,iPhone 8,iPhone X | 667.0 | 216.0 | 162.0

iPhone 6 plus,iPhone 7 plus,iPhone 8 plus | 736.0 | 226.0 | 162.0

iPad第5代,iPad Air,iPad Air 2,iPad Pro 9.7,iPad Pro 10.5,iPad Pro 12.9 | 1024.0 | 265.0 | 353.0

谢谢!

答案 6 :(得分:1)

我创建了这个表格,其中包含 iPhone 和 iPad 键盘的高度,横向和纵向模式,工具栏都打开和关闭。

我什至解释了如何在代码 here 中使用这些维度。

请注意,只有在布局视图之前需要知道键盘的尺寸时,才应使用这些尺寸。否则,其他答案中的解决方案效果更好。

enter image description here

答案 7 :(得分:0)

iPhone

KeyboardSizes:

  1. 5S,SE,5、5C(320×568) keyboardSize =(0.0、352.0、320.0、216.0) keyboardSize =(0.0,315.0,320.0,253.0)

2.6S,6,7,8:(375×667): keyboardSize =(0.0,407.0,375.0,260。

3.6 +,6S +,7+,8+:(414×736) keyboardSize =(0.0,465.0,414.0,271.0)

4.XS,X:(375 X 812) keyboardSize =(0.0,477.0,375.0,335.0)

5.XR,XSMAX((414 x 896) keyboardSize =(0.0,550.0,414.0,346.0)

相关问题