and와 다르게 ios에서는 webview의 text입력창에 포커스가 가 키보드가 올라오더라도 웹뷰가 줄어들지 않는다. 그래서 title이 안보이거나 bottom의 입력창이 가려지는 등의 문제가 있다. 이를 감지해서 웹뷰 사이즈를 줄여줄 때 이용
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if keyboard == false{
keyboard = true
lastKeyboardHeight = keyboardSize.height
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height)
}
}
@objc func keyboardWillChange(notification: NSNotification) {
let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
if keyboard == true && lastKeyboardHeight != keyboardSize1.height {
if lastKeyboardHeight < keyboardSize1.height{
let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight
chatDetailView.frame.origin.y -= keyboardDifference
} else {
let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height
chatDetailView.frame.origin.y += keyboardDifference
}
lastKeyboardHeight = keyboardSize1.height
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if keyboard == true {
keyboard = false
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height)
}
}