我的ViewController上有两个UItextField。第一个位于ViewController的上半部分,但第二个位于底部。
我设法让键盘在返回时隐藏,但是当我点击第二个(下方)UItextField时,键盘出现并覆盖它。这意味着不再可能看到您正在键入的内容。
如何在单击第二个UItextField时向上移动ViewController,以便用户可以看到他们正在键入的内容,然后在用户按下返回时将ViewController向下移动?
答案 0 :(得分:0)
使用 scrollView 并在键盘显示时滚动文本框
答案 1 :(得分:0)
在您的 Hole View(ViewController 视图)中添加 scrollView 然后添加隐藏和显示键盘的通知
当键盘将通过减去键盘隐藏显示更改 scrollView 内容插入时:scrollView.ContentInsets.height - keyboard.height:
func registerForKeyboardNotifications() {
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIWindow.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIWindow.keyboardWillHideNotification, object: nil)
}
@objc
func keyboardWillShow(notification: NSNotification) {
guard let keyboareRect = notification.userInfo?[UIWindow.keyboardFrameBeginUserInfoKey] as? CGRect else { return }
let keyboardSize = keyboareRect.size
let insets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
scrollView.contentInset = insets
scrollView.scrollIndicatorInsets = insets
var aRect = self.view.frame
aRect.size.height -= keyboardSize.height
guard let activeTextField = self.activeTextField else { return }
if !aRect.contains(activeTextField.frame.origin) {
scrollView.scrollRectToVisible(activeTextField.frame, animated: true)
}
}
@objc
func keyboardWillHide(notification: NSNotification) {
scrollView.contentInset = UIEdgeInsets.zero
scrollView.scrollIndicatorInsets = UIEdgeInsets.zero
}