0

I recently started to use IQKeyboardManager in my app that I am working on, it works really well except for a little problem. The height of my text view increases when the keyboard appears(Which makes the text view go up), I know this because I am printing the height of the text view each time I click on the text view. Here is the code of my text view:

// 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

}

Specifying the height of the text view solves this problem but, it will create another problem when the app is used on other devices with different resolutions.

// 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)
}

Here is a gif to help you understand better.

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdullah Ajmal
  • 431
  • 5
  • 17

1 Answers1

1

Don't worry, It is IQKeyboardManager's feature, not an error.

You use IQKeyboardManager, because you want the textView go up, when textViewDidBeginEditing.


The source code is very clear.

When you activate IQKeyboardManager, in IQKeyboardManager.swift, you call override init() {, override init() { do registerAllNotifications ,registerAllNotifications do Registering for UITextField notification.

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

// ...

keyboard is already showing. adjust position.

In method func optimizedAdjustPosition()(), the height changes


Your code is weird.

why startStoryTextView.bottomAnchor is related to view.safeAreaLayoutGuide.bottomAnchor.

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

Since startStoryTextView.superView is popUp

popUp.addSubview(startStoryTextView)

I think it is code error. IQKeyboardManager unrelated.

dengApro
  • 3,848
  • 2
  • 27
  • 41
  • Why does the height increase tho?? This error only happens when using IQKeyboardManager – Abdullah Ajmal Jun 29 '19 at 02:23
  • I've added a gif to help you understand better, hope you can help me out, thanks. – Abdullah Ajmal Jun 29 '19 at 02:31
  • And I added the constraints code of the popup view too, as you can see the bottom anchor of the popup view goes below the bottom anchor of the view, this was why I had to put the code as this: startStoryTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -70).isActive = true – Abdullah Ajmal Jun 29 '19 at 02:54
  • BTW, this problem is only when I use view.safeAreaLayoutGuide.bottomAnchor, but when I use view.bottomAnchor, there is no problem. – Abdullah Ajmal Jun 29 '19 at 14:02
  • Congratulations, You can also disable IQKeyboardManager in this viewController – dengApro Jun 29 '19 at 14:11
  • I don't want to disable IQKeyboardManager because I wanna use it when typing on the text view but I just want to know why this happens only when I use safeAreaLayoutGuide, and do you know any other methods to fix this? – Abdullah Ajmal Jun 29 '19 at 14:26