iOS鍵盤監聽以及輸入框位置調整

首先設置系統廣播監聽鍵盤變化

  NotificationCenter.default.addObserver(self, selector: #selector(keyboardChange(notify:)), name:UIResponder.keyboardWillChangeFrameNotification , object: nil)
    }

把輸入框的底部約束拖動到文件中
鍵盤事情響應修改輸入框的底部約束

//   鍵盤監聽事件
   @objc func keyboardChange(notify:NSNotification)
   {
       // 1.取出鍵盤最終的rect
       let value = notify.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue
       // 鍵盤彈出時的Y值
       let rectY = value.cgRectValue.origin.y
       // 2.修改工具條的約束
       let height = UIScreen.main.bounds.height
       let margin = height - rectY
       TabBarViewBottom.constant = margin
       // 3.更新界面
       let duration = notify.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber
       UIView.animate(withDuration: TimeInterval(duration)) {
           self.view.layoutIfNeeded()
       }
       
   }

鍵盤彈出輸入框效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章