首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键盘出现时调整UITextView大小

键盘出现时调整UITextView大小
EN

Stack Overflow用户
提问于 2021-01-12 01:02:56
回答 1查看 88关注 0票数 0

当键盘出现时,我尝试调整UITextView-field的高度(iOS 14.2,Xcode12.3)。从UITextView底部到保存区的距离是90,因此它的下部被键盘隐藏,在编辑时看不到。

我使用这里显示的解决方案进行了尝试:Resize the UITextView when keyboard appears

相应地,我的代码如下:

代码语言:javascript
复制
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!

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

多谢你们的支持

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-12 03:04:19

我不能通过使用content inset想出一个解决方案,但我可以提出另一种方法。

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

代码语言:javascript
复制
@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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65671341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档