Swift3.0 鍵盤高度監聽獲取


方法:通過通知監聽鍵盤的動態

1.鍵盤的動態有四種:

 public static let UIKeyboardWillShow: NSNotification.Name

    public static let UIKeyboardDidShow: NSNotification.Name

    public static let UIKeyboardWillHide: NSNotification.Name

    public static let UIKeyboardDidHide: NSNotification.Name

2.下面監聽下彈出之前,和回收之前的事件

//鍵盤彈起
    @objc private func willShow(nottification:NSNotification){
        
        let info:NSDictionary = nottification.userInfo as NSDictionary!
//        print(info)
       //通過輸出看到下面的信息
    
         /*
Optional([AnyHashable("UIKeyboardCenterBeginUserInfoKey"): NSPoint: {207, 849},
            
AnyHashable("UIKeyboardIsLocalUserInfoKey"): 1,
           
AnyHashable("UIKeyboardCenterEndUserInfoKey"): NSPoint: {207, 623},
            
AnyHashable("UIKeyboardBoundsUserInfoKey"): NSRect: {{0, 0}, {414, 226}},
            
AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 510}, {414, 226}},
                     
AnyHashable("UIKeyboardAnimationCurveUserInfoKey"): 7,
           
AnyHashable("UIKeyboardFrameBeginUserInfoKey"): NSRect: {{0, 736}, {414, 226}},
            
AnyHashable("UIKeyboardAnimationDurationUserInfoKey"): 0.25])
*/ //取出鍵盤的高度 let keyValue = info.object(forKey: "UIKeyboardFrameEndUserInfoKey") let keyRect = (keyValue as AnyObject).cgRectValue let height = keyRect?.size.height print(height!) } //鍵盤收起 @objc private func willHide(nottification:NSNotification){ }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM