繁体   English   中英

出现键盘时调整 UITextView 的大小

[英]Resize UITextView when keyboard appears

当键盘出现时,我尝试调整 UITextView 字段的高度(iOS 14.2、Xcode 12.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!

不幸的是,当键盘出现并且文本部分隐藏时,插入大小没有改变。 有谁知道,为什么它不起作用?

谢谢你的支持

我无法通过使用内容插图提出解决方案,但我可以提出另一种方法。

如果将底部约束添加到 textView 并为此创建出口,则可以在通知中更改其常量值;

@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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM