eag*_*arn 8 iphone keyboard cocoa-touch ios
我正试图获得iOS键盘的高度.我已经完成并使用了涉及订阅通知的方法,如下所示:https: //gist.github.com/philipmcdermott/5183731
- (void)viewDidAppear:(BOOL) animated {
[super viewDidAppear:animated];
// Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];
// Do something with keyboard height
}
- (void)keyboardWillHide:(NSNotification *)notification {
CGRect keyboardBounds;
[[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];
// Do something with keyboard height
}
Run Code Online (Sandbox Code Playgroud)
这适用于用户实际显示键盘的情况.
我的问题:我有另一种观点,我们称它为micView,可以在键盘出现之前显示.用户可以在键入之前选择使用麦克风.我希望micView与键盘的高度相同,这就是我需要键盘高度的原因,但是在键盘被迫出现之前我需要它.因此,在我需要读取高度值之前,未达到UIKeyboardWillShowNotification.
我的问题是:如何在没有键盘出现的情况下通过Notifications或其他方法获得键盘的高度.
我考虑过明确强制键盘出现在viewDidLoad中,这样我就可以将一个实例变量设置为该值,然后将其隐藏并删除两者的动画.但这真的是唯一的方法吗?
Dun*_*age 10
This Swift class provides a turn-key solution that manages all the necessary notifications and initializations, letting you simply call a class method and have returned the keyboard size or height.
Calling from Swift:
let keyboardHeight = KeyboardService.keyboardHeight()
let keyboardSize = KeyboardService.keyboardSize()
Run Code Online (Sandbox Code Playgroud)
Calling from Objective-C:
CGFloat keyboardHeight = [KeyboardService keyboardHeight];
CGRect keyboardSize = [KeyboardService keyboardSize];
Run Code Online (Sandbox Code Playgroud)
If wanting to use this for initial view layout, call this from the viewWillAppear method of a class where you want the keyboard height or size before the keyboard appears. It should not be called in viewDidLoad, as a correct value relies on your views having been laid out. You can then set an autolayout constraint constant with the value returned from the KeyboardService, or use the value in other ways. For instance, you might want to obtain the keyboard height in prepareForSegue to assist in setting a value associated with the contents of a containerView being populated via an embed segue.
Note re safe area, keyboard height, and iPhone X:
The value for keyboard height returns the full height of the keyboard, which on the iPhone X extends to the edge of the screen itself, not just to the safe area inset. Therefore, if setting an auto layout constraint value with the returned value, you should attach that constraint to the superview bottom edge, not to the safe area.
注重硬件键盘模拟器:
当硬件键盘连接,这个代码将提供硬件键盘,也就是没有高度的屏幕上的高度。当然,确实需要考虑此状态,因为它模拟了将硬件键盘连接到实际设备时会发生的情况。因此,您期望键盘高度的布局需要适当地响应键盘高度零。
KeyboardService类:
与往常一样,如果从Objective-C进行调用,则只需要将应用程序的Swift桥接标头导入到MyApp-Swift.hObjective-C类中。
import UIKit
class KeyboardService: NSObject {
static var serviceSingleton = KeyboardService()
var measuredSize: CGRect = CGRect.zero
@objc class func keyboardHeight() -> CGFloat {
let keyboardSize = KeyboardService.keyboardSize()
return keyboardSize.size.height
}
@objc class func keyboardSize() -> CGRect {
return serviceSingleton.measuredSize
}
private func observeKeyboardNotifications() {
let center = NotificationCenter.default
center.addObserver(self, selector: #selector(self.keyboardChange), name: .UIKeyboardDidShow, object: nil)
}
private func observeKeyboard() {
let field = UITextField()
UIApplication.shared.windows.first?.addSubview(field)
field.becomeFirstResponder()
field.resignFirstResponder()
field.removeFromSuperview()
}
@objc private func keyboardChange(_ notification: Notification) {
guard measuredSize == CGRect.zero, let info = notification.userInfo,
let value = info[UIKeyboardFrameEndUserInfoKey] as? NSValue
else { return }
measuredSize = value.cgRectValue
}
override init() {
super.init()
observeKeyboardNotifications()
observeKeyboard()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
Run Code Online (Sandbox Code Playgroud)
头点头:
此处的observeKeyboard方法基于Peres在Objective-C对此问题的回答中概述的原始方法。
您可以使用的快速解决方案与您想要缓存键盘时使用的快速解决方案相同(第一次显示时,会稍有延迟......).图书馆在这里.有趣的一点:
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:field];
[field becomeFirstResponder];
[field resignFirstResponder];
[field removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)
所以基本上是显示它然后隐藏它.你可以听取通知,只是在height没有实际看到它的情况下获得通知.额外奖励:你可以缓存它.:)