当键盘出现时,我尝试调整UITextView-field的高度(iOS 14.2,Xcode12.3)。从UITextView底部到保存区的距离是90,因此它的下部被键盘隐藏,在编辑时看不到。
我使用这里显示的解决方案进行了尝试:Resize the UITextView when keyboard appears
相应地,我的代码如下:
class EditierenVC: UIViewController, UITextFieldDelegate, UITextViewDelegate {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(false)
NotificationCenter.default.addObserver(
self,
selector: #selector(EditierenVC.handleKeyboardDidShow(notification:)),
name: UIResponder.keyboardDidShowNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(EditierenVC.handleKeyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil
)
}
@objc func handleKeyboardDidShow(notification: NSNotification) {
guard let endframeKeyboard = notification
.userInfo![UIResponder.keyboardFrameEndUserInfoKey]
as? CGRect else { return }
textfeld.contentInset = UIEdgeInsets(
top: 0.0,
left: 0.0,
bottom: endframeKeyboard.size.height-85,
right: 0.0
)
view.layoutIfNeeded()
}
@objc func handleKeyboardWillHide() {
textfeld.contentInset = .zero
view.layoutIfNeeded()
}
//**************************
// MARK: - Views
//**************************
@IBOutlet weak var textfeld: UITextView!不幸的是,当键盘出现并且文本部分隐藏时,插入的大小没有改变。有没有人知道为什么它不起作用?
多谢你们的支持
发布于 2021-01-12 03:04:19
我不能通过使用content inset想出一个解决方案,但我可以提出另一种方法。
如果向textView添加底部约束并为其创建outlet,则可以在通知中更改其常量值;
@objc func handleKeyboardDidShow(notification: NSNotification) {
guard let endframeKeyboard = notification
.userInfo![UIResponder.keyboardFrameEndUserInfoKey]
as? CGRect else { return }
textViewBottomConstraint.constant = endframeKeyboard.size.height-85
}
@objc func handleKeyboardWillHide() {
textViewBottomConstraint.constant = // set the previous value here
}https://stackoverflow.com/questions/65671341
复制相似问题