gpt4 book ai didi

swift - Swift 中从 userInfo 获取键盘大小

转载 作者:行者123 更新时间:2023-11-30 14:01:19 25 4
gpt4 key购买 nike

我一直在尝试添加一些代码,以便在键盘出现时向上移动我的 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 native 字典,因此您必须使用字典下标语法 (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/32950732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com