我一直在尝试添加一些代码,以便在键盘出现时向上移动我的 View ,但是,我在尝试将 Objective-C 示例转换为 Swift 时遇到了问题。我取得了一些进展,但我被困在一条特定的线上。
这是我一直关注的两个教程/问题:
How to move content of UIViewController upwards as Keypad appears using Swift http://www.ioscreator.com/tutorials/move-view-when-keyboard-appears
这是我目前拥有的代码:
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) {
//
}
目前,我在这一行遇到错误:
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
如果有人能让我知道这行代码应该是什么,我应该设法自己弄清楚其余部分。
最佳答案
你的线路有一些问题:
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
notification.userInfo
返回一个可选字典[NSObject : AnyObject]?
, 因此在访问其值之前必须对其进行解包。- Objective-C
NSDictionary
映射到 Swift 原生字典,所以你必须 使用字典下标语法 (dict[key]
) 访问值。 - 该值必须转换为
NSValue
,以便您可以对其调用CGRectValue
。
所有这些都可以通过可选赋值、可选链接和可选强制转换的组合来实现:
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 设备上首次显示后更改初始渲染高度。
关于swift - 在 Swift 中从 userInfo 获取键盘大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25451001/