Swift 鍵盤顯示與隱藏通知封裝SKeyBoard

Skeyboard.swift

struct SKeyBoard {
    
    // 注冊鍵盤出現(xiàn)
    static func registerKeyBoardShow(target: UIViewController) {
        NSNotificationCenter.defaultCenter().addObserver(target, selector: "keyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil)
    }
    
    // 注冊鍵盤隱藏
   static func registerKeyBoardHide(target: UIViewController) {
        NSNotificationCenter.defaultCenter().addObserver(target, selector: "keyboardWillHideNotification:", name: UIKeyboardWillHideNotification, object: nil)
    }
    
    // 移除鍵盤出現(xiàn)通知
    static func removeKeyboardNotifications(target: UIViewController) {
        NSNotificationCenter.defaultCenter().removeObserver(target, name: UIKeyboardWillShowNotification, object: nil)
    }
    
    // 移除鍵盤隱藏通知
    static func removeKeyboardHideNotifications(target: UIViewController) {
        NSNotificationCenter.defaultCenter().removeObserver(target, name: UIKeyboardWillHideNotification, object: nil)
    }
    
    // 返回鍵盤高度
   static func getKeyboardHeight(notification: NSNotification) -> CGFloat {
        let userInfo = notification.userInfo
        let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
        
        return keyboardSize.CGRectValue().height
    }
    
    // 返回鍵盤上拉動畫持續(xù)時間
   static func getKeyBoardDuration(notification: NSNotification) -> Double {
        let userInfo = notification.userInfo
        let keyboardDuration = userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
        
        return keyboardDuration
    }
    
    // 返回鍵盤動畫曲線
   static func getKeyBoardAnimationCurve(notification: NSNotification) -> NSObject {
        let userInfo = notification.userInfo
        let keyboardTranstionAnimationCurve = userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSValue
    
        return keyboardTranstionAnimationCurve
    }
    
    
}

使用

1.注冊通知

    SKeyBoard.registerKeyBoardShow(self)
    SKeyBoard.registerKeyBoardHide(self)

2.必須實現(xiàn)以下2個方法,否則程序會崩潰

func keyboardWillShowNotification(notification:NSNotification){
    let rect = SKeyBoard.getKeyboardHeight(notification)
    self.textView.contentInset = UIEdgeInsetsMake(0, 0, rect, 0)
    
}

func keyboardWillHideNotification(notification: NSNotification) {
    self.textView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,602評論 19 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,414評論 4 61
  • 經(jīng)常聽到這樣一句話:“女人,一定要矜持,這樣別人才會珍惜你!” 這句話我贊同,不過我有另一種看法。 前天我上輔全,...
    girl揮著翅膀的女孩閱讀 11,866評論 1 3
  • 忙了一天,很晚才到家。哄完孩子睡覺,再坐到書桌前,已近半夜。 今天的寫作還沒有,寫點什么呢?腦中空無一物。 這兩天...
    淺瀾淺瀾閱讀 230評論 0 0

友情鏈接更多精彩內(nèi)容