0
나는 위쪽, 아래쪽, 꼬리말 및 꼬리표가 달린 전체 화면 UITableView를 가지고 있습니다. 이 ViewController는 내비게이션 컨트롤러 안에 있습니다. 테이블의 맨 아래쪽이 위로 이동하고 나타나는대로 키보드로 움직이기를 원합니다. 다음 코드를 가지고 있습니다 :키보드가 나타나면 UITableView의 크기를 조절하십시오. 애니메이션은 육포입니다.
// MARK: Keyboard
func registerObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func unregisterObservers() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(_ notification: Notification) {
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardAnimationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
let keyboardAnimationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
tableViewBottomConstraint.constant = keyboardFrame.height
UIView.animate(withDuration: TimeInterval(keyboardAnimationDuration), delay: 0, options: [UIViewAnimationOptions(rawValue: UInt(keyboardAnimationCurve))], animations: {
self.view.layoutIfNeeded()
}, completion: { (finished: Bool) in
})
}
func keyboardWillHide(_ notification: Notification) {
let keyboardAnimationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
let keyboardAnimationCurve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
tableViewBottomConstraint.constant = 0
UIView.animate(withDuration: TimeInterval(keyboardAnimationDuration), delay: 0, options: [UIViewAnimationOptions(rawValue: UInt(keyboardAnimationCurve))], animations: {
self.view.layoutIfNeeded()
}, completion: { (finished: Bool) in
})
}
ViewDidLoad에 옵저버를 등록합니다. 키보드가 나타나면 애니메이션이 상당히 흔들리는 것처럼 보입니다. 그러나 해산 애니메이션은 어떤 문제도없는 것처럼 보입니다. 여기서 내가 뭘 잘못하고 있니? 애니메이션 기간 또는 커브를 잘못 설정 했습니까?
자동 –
이 볼 일을 http://stackoverflow.com/questions/594181/making-a-uitableview-scroll-when-text-field-is-selected합니다 –
tableView 제약 조건을 이동하는 대신 tableView의 contentOffset을 이동하는 것을 고려하십시오. 훨씬 더 좋을 것입니다. – Tj3n