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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11284321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:05:08  来源:igfitidea点击:

What is the height of iPhone's onscreen keyboard?

iosiphonekeyboardheight

提问by Erik B

The height in portrait and the height in landscape measured in points.

纵向高度和横向高度以磅为单位测量。

采纳答案by Erik B

Keyboard height is 216pts for portrait mode and 162pts for Landscape mode.

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

Source

来源

回答by Ken Anderson

I used the following approach for determining the frame of the keyboard in iOS 7.1.

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

In the init method of my view controller, I registered for the UIKeyboardDidShowNotification:

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

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

Then, I used the following code in keyboardOnScreen:to gain access to the frame of the keyboard. This code gets the userInfodictionary from the notification and then accesses the NSValueassociated with UIKeyboardFrameEndUserInfoKey. You can then access the CGRect and convert it to the coordinates of the view of your view controller. From there, you can perform any calculations you need based on that frame.

然后,我使用以下代码keyboardOnScreen:来访问键盘的框架。此代码userInfo从通知中获取字典,然后访问NSValueUIKeyboardFrameEndUserInfoKey. 然后,您可以访问 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

迅速

And the equivalent implementation with 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)")
}

回答by Mike Gledhill

Do remember that, with iOS 8, the onscreen keyboard's size canvary. Don't assume that the onscreen keyboard will always be visible (with a specificheight) or invisible.

请记住,在 iOS 8 中,屏幕键盘的大小可能会有所不同。不要假设屏幕键盘始终可见(具有特定高度)或不可见。

Now, with iOS 8, the user can also swipe the text-prediction area on and off... and when they do this, it would kick off an app's keyboardWillShowevent again.

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

This willbreak a lot of legacy code samples, which recommended writing a keyboardWillShowevent, which merely measures the current height of the onscreen keyboard, and shifting your controls up or down on the page by this (absolute) amount.

破坏许多遗留代码示例,这些示例建议编写一个keyboardWillShow事件,它仅测量屏幕键盘的当前高度,并将您的控件在页面上向上或向下移动此(绝对)量。

enter image description here

在此处输入图片说明

In other words, if you see any sample code, which just tells you to add a keyboardWillShowevent, measure the keyboard height, then resize your controls' heights by this amount, this will no longer always work.

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

In my example above, I used the sample code from the following site, which animates the vertical constraints constantvalue.

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

Practicing AutoLayout

练习自动布局

In my app, I added a constraint to my UITextView, set to the bottom of the screen. When the screen first appeared, I stored this initial vertical distance.

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

Then, whenever my keyboardWillShowevent gets kicked off, I addthe (new) keyboard height to this original constraint value (so the constraintresizes the control's height).

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

enter image description here

在此处输入图片说明

Yeah. It's ugly.

是的。它很丑。

And I'm a little annoyed/surprised that XCode 6's horribly-painful AutoLayout doesn't just allow us to attach the bottoms of controls to either the bottom of the screen, or the top of onscreen keyboard.

我有点恼火/惊讶的是,XCode 6 令人痛苦的 AutoLayout 不仅允许我们将控件的底部附加到屏幕底部或屏幕键盘顶部。

Perhaps I'm missing something.

也许我错过了一些东西。

Other than my sanity.

除了我的理智。

回答by Philipp Sander

version note: this is no longer value in iOS 9 & 10, as they support custom keyboard sizes.

版本说明:这在 iOS 9 和 10 中不再有价值,因为它们支持自定义键盘尺寸。

This depends on the model and the QuickType bar:

这取决于型号和 QuickType 栏:

enter image description here

在此处输入图片说明

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

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

回答by torcelly

The keyboard height depends on the model, the QuickType bar, user settings... The best approach is calculate dinamically:

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

Swift 3.0

斯威夫特 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
           }
    }

回答by Seungyoun Yi

I can't find latest answer, so I check it all with simulator.(iOS 11.0)

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



Device | Screen Height | Portrait | Landscape

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

iPhone 4s | 480.0 | 216.0 | 162.0

iPhone 4s | 480.0 | 216.0 | 162.0

iPhone 5, iPhone 5s, iPhone SE | 568.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、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

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

iPad 5th generation, iPad Air, iPad Air 2, iPad Pro 9.7, iPad Pro 10.5, iPad Pro 12.9 | 1024.0 | 265.0 | 353.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



Thanks!

谢谢!

回答by Tej27

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)
  1. 5S, SE, 5, 5C (320 × 568) 键盘尺寸 = (0.0, 352.0, 320.0, 216.0) 键盘尺寸 = (0.0, 315.0, 320.0, 253.0)

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

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)

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)

4.XS, X :(375 X 812) 键盘尺寸 = (0.0, 477.0, 375.0, 335.0)

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

5.XR,XSMAX((414 x 896) 键盘尺寸 = (0.0, 550.0, 414.0, 346.0)