【问题标题】:Height of the UITextView increases when using IQKeyboardManagers使用 IQKeyboardManagers 时 UITextView 的高度会增加
【发布时间】:2019-11-08 15:31:02
【问题描述】:

我最近开始在我正在开发的应用程序中使用 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 swift uitextview iqkeyboardmanager


    【解决方案1】:

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

    您使用IQKeyboardManager,因为您希望textView 上升,而textViewDidBeginEditing


    源码很清楚。

    当您激活IQKeyboardManager 时,在 IQKeyboardManager.swift 中,您调用 override init() {, override init() { do registerAllNotifications ,registerAllNotifications 注册 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, constant: -70).isActive = true

    因为startStoryTextView.superViewpopUp

    popUp.addSubview(startStoryTextView)

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

    【讨论】:

    • 为什么高度增加了?此错误仅在使用 IQKeyboardManager 时发生
    • 我添加了一个gif来帮助你更好地理解,希望你能帮助我,谢谢。
    • 我还添加了弹出视图的约束代码,您可以看到弹出视图的底部锚点位于视图的底部锚点下方,这就是为什么我必须将代码设置为这个:startStoryTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -70).isActive = true
    • 顺便说一句,这个问题只有在我使用view.safeAreaLayoutGuide.bottomAnchor的时候才有,但是当我使用view.bottomAnchor的时候就没有问题了。
    • 恭喜你,你也可以在这个viewController中禁用IQKeyboardManager
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多