java实现键盘移动图片,快速移动视图与键盘
我試圖在鍵盤(pán)出現(xiàn)時(shí)向上移動(dòng)視圖,在鍵盤(pán)隱藏時(shí)向下移動(dòng) .
我遇到的問(wèn)題是高度似乎不同:KBH1輸出216,KBH2輸出260 .
這導(dǎo)致視圖向下移動(dòng)得比最初移動(dòng)的距離更遠(yuǎn) .
我該如何糾正這個(gè)?
extension UIViewController {
func addKeyboardFunctions() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboardEx))
// tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillAppear(_ notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
print("KBH: \(keyboardSize.height)")
}
}
}
@objc func keyboardWillDisappear(_ notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
print("KBH: \(keyboardSize.height)")
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java实现键盘移动图片,快速移动视图与键盘的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle leg函数,oracle对
- 下一篇: python从爬虫到数据分析项目_零基础