簡體   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