我最近开始在我正在开发的应用程序中使用IQKeyboardManager,它确实工作得很好,除了一个小问题。出现键盘时,文本视图的高度会增加(这会使文本视图向上移动),我知道这一点是因为每次单击文本视图时,都会打印文本视图的高度。这是我的文本视图的代码:

// Setup text view
func setupTextView() {

    // Placeholder text and color
    startStoryTextView.textColor = .gray
    startStoryTextView.text = "Type here"
    startStoryTextView.backgroundColor = .red
    // Add some padding to the text insde the text view
    startStoryTextView.textContainerInset = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
    startStoryTextView.font = UIFont(name: "PatrickHand-Regular", size: 23)
    startStoryTextView.layer.cornerRadius = 25

    popUp.addSubview(startStoryTextView)
    addTextViewConstraints()
}
// Add the constraints to the text view
func addTextViewConstraints() {

    startStoryTextView.translatesAutoresizingMaskIntoConstraints = false
    startStoryTextView.leadingAnchor.constraint(equalTo: popUp.leadingAnchor, constant: 3).isActive = true
    startStoryTextView.trailingAnchor.constraint(equalTo: popUp.trailingAnchor, constant: -3).isActive = true
    startStoryTextView.topAnchor.constraint(equalTo: popUp.topAnchor, constant: 3).isActive = true
    //startStoryTextView.heightAnchor.constraint(equalToConstant: 589).isActive = true
    startStoryTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -70).isActive = true
}

// Add constraints to the pop up view
func addPopUpConstraints() {

    popUp.translatesAutoresizingMaskIntoConstraints = false
    popUp.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
    popUp.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 10).isActive = true
    popUp.topAnchor.constraint(equalTo: view.topAnchor, constant: 200).isActive = true
    popUp.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

}


指定文本视图的高度可以解决此问题,但是当在具有不同分辨率的其他设备上使用该应用程序时,它将创建另一个问题。

// To print the height of the text view every time the user clicks on the text view
func textViewDidBeginEditing(_ textView: UITextView) {
    print(startStoryTextView.frame.height)
}


ios - 使用IQKeyboardManagers时UITextView的高度增加-LMLPHP

最佳答案

不用担心,这是IQKeyboardManager的功能,不是错误。

您使用IQKeyboardManager,因为您希望当textViewDidBeginEditing时textView上升。



源代码很清楚。

激活IQKeyboardManager时,在IQKeyboardManager.swift中,调用override init() {override init() { do registerAllNotificationsregisterAllNotifications进行UITextField通知的注册。

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShowDng(_:)), name: UIKeyboardWillShow, object: nil)


// ...

键盘已经显示。调整位置。

在方法func optimizedAdjustPosition()()中,高度改变



您的代码很奇怪。

为什么startStoryTextView.bottomAnchorview.safeAreaLayoutGuide.bottomAnchor相关。


  startStoryTextView.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor,常量:-70).isActive = true


由于startStoryTextView.superViewpopUp


  popUp.addSubview(startStoryTextView)


我认为这是代码错误。 IQKeyboardManager无关。

关于ios - 使用IQKeyboardManagers时UITextView的高度增加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56776991/

10-08 22:34