在 Swift 中从 userInfo 获取键盘大小 [英] Getting keyboard size from userInfo in Swift
问题描述
我一直在尝试添加一些代码以在键盘出现时向上移动我的视图,但是,我在尝试将 Objective-C 示例转换为 Swift 时遇到了问题.我取得了一些进展,但我被困在一个特定的行上.
I have been trying to add some code to move my view up when the keyboard appears, however, I am having issues trying to translate the Objective-C examples into Swift. I have made some progress, but I am stuck on one particular line.
这是我一直在关注的两个教程/问题:
These are the two tutorials/questions I have been following:
如何移动使用 Swift 出现键盘时 UIViewController 向上http://www.ioscreator.com/tutorials/move-view-when-键盘出现
这是我目前拥有的代码:
Here is the code I currently have:
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillShow(notification: NSNotification) {
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
let frame = self.budgetEntryView.frame
frame.origin.y = frame.origin.y - keyboardSize
self.budgetEntryView.frame = frame
}
func keyboardWillHide(notification: NSNotification) {
//
}
目前,我在这一行遇到错误:
At the moment, I am getting an error on this line:
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
如果有人能让我知道这行代码应该是什么,我应该设法自己弄清楚其余的.
If someone could let me know what this line of code should be, I should manage to figure out the rest myself.
推荐答案
您的线路存在一些问题:
There are some problems in your line:
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
notification.userInfo
返回一个可选字典[NSObject : AnyObject]?
,所以在访问它的值之前它必须被解包.- Objective-C
NSDictionary
映射到 Swift 原生字典,所以你必须使用字典下标语法 (dict[key]
) 访问值. - 必须将该值转换为
NSValue
,以便您可以对其调用CGRectValue
. notification.userInfo
returns an optional dictionary[NSObject : AnyObject]?
, so it must be unwrapped before accessing its values.- The Objective-C
NSDictionary
is mapped to a Swift native Dictionary, so you must use the dictionary subscript syntax (dict[key]
) to access the values. - The value must be cast to
NSValue
so that you can callCGRectValue
on it.
所有这些都可以通过结合可选赋值、可选链和可选强制转换来实现:
All this can be achieved with a combination of optional assignment, optional chaining and optional casts:
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
// ...
} else {
// no UIKeyboardFrameBeginUserInfoKey entry in userInfo
}
} else {
// no userInfo dictionary in notification
}
或者一步:
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
// ...
}
Swift 3.0.1 (Xcode 8.1) 的更新:
if let userInfo = notification.userInfo {
if let keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
// ...
} else {
// no UIKeyboardFrameBeginUserInfoKey entry in userInfo
}
} else {
// no userInfo dictionary in notification
}
或者一步:
if let keyboardSize = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect {
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
// ...
}
Swift 5 (Xcode 11.6) 的更新:
guard let userInfo = notification.userInfo,
let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
我建议使用 keyboardFrameEndUserInfoKey
而不是 keyboardFrameBeginUserInfoKey
因为键盘会在较旧的 iOS 设备上首次显示后更改初始渲染高度.
I recommend using keyboardFrameEndUserInfoKey
instead of keyboardFrameBeginUserInfoKey
since the keyboard changes the initial render height after the first display on older iOS devices.
这篇关于在 Swift 中从 userInfo 获取键盘大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!