繁体   English   中英

如何使用Swift查找键盘高度

[英]How to find keyboard height using Swift

let frame = (info[UIKeyboardFrameEndUserInfoKey] as NSValue) as CGRect

不起作用。 有谁知道为什么这不能按预期运作?

错误:'NSValue'无法转换为'CGRect'

请记住以下内容:

let curve = (info[UIKeyboardAnimationCurveUserInfoKey] as NSValue) as UInt

工作良好。

编辑:即使Apple文档表明这是一个小问题,因为在Cocoa中对UIKeyboardAnimationCurveUserInfoKey的以下评论

// NSValue of CGRect

Edit2:调试器将其定义为NSConcreteValue。 如何将其转换为CGRect?

let frame = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()

在NSNotificationCenter中使用keyboardFrame高度

override func viewWillAppear(animated: Bool) {


        NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector: #selector(self.keyboardWasShown(_:)),
                                                         name: UIKeyboardWillChangeFrameNotification,
                                                         object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector: #selector(self.keyboardWillHide(_:)),
                                                         name: UIKeyboardWillHideNotification,
                                                         object: nil)


    }



    func keyboardWasShown(notification: NSNotification) {
        let info = notification.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.constBottomLayout.constant = keyboardFrame.size.height
        })
    }


    func keyboardWillHide(sender: NSNotification) {
        // self.view.frame.origin.y = 0
        self.constBottomLayout.constant = 0
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM