从Swift中的userInfo获取键盘大小

从Swift中的userInfo获取键盘大小,第1张

概述我一直在试图添加一些代码来移动我的视图,当键盘出现,但是,我有问题,试图将Objective-C的例子翻译成Swift。我已经取得了一些进展,但我被困在一条线。 这是我一直在关注的两个教程/问题: How to move content of UIViewController upwards as Keypad appears using Swift http://www.ioscreator.c 我一直在试图添加一些代码来移动我的视图,当键盘出现,但是,我有问题,试图将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,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,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,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,right: 0)    // ...}
总结

以上是内存溢出为你收集整理的从Swift中的userInfo获取键盘大小全部内容,希望文章能够帮你解决从Swift中的userInfo获取键盘大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060791.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存