swift2 語法 語法 判斷鍵盤高度

liangtongzhuo發表於2017-12-14
  • 語法2.2

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MusicViewController.keyboardWasShown(_:)), name: UIKeyboardDidShowNotification, object: nil)
    複製程式碼
  • 語法2.0

      NSNotificationCenter.defaultCenter().addObserver(self, selector:      "keyboardWillDisappear:", name:UIKeyboardWillHideNotification, object: nil)
    
    
    
        func keyboardWasShown(notification: NSNotification) {
    
      // 獲取鍵盤資訊
      let keyboardinfo = notification.userInfo![UIKeyboardFrameBeginUserInfoKey]
    
      let keyboardheight:CGFloat = (keyboardinfo?.CGRectValue.size.height)!
    
      print("鍵盤彈起")
    
      print(keyboardheight)
    複製程式碼

    }

    func keyboardWillDisappear(notification:NSNotification){

      print("鍵盤落下")
    複製程式碼

    }