Dynamic UITextView mislocation behavior
Asked Answered
M

3

8

I am trying to have a textview similar to iPhone messages, where the textview initially has a constraint (height <= 100) and the scrollEnabled = false

This is a link to the project: https://github.com/akawther/TextView

The text view increases in height based on the content size as in the image on the left until it reaches the height of 100, then the scrollEnabled is set to true. It works perfectly until I click the "send" button on the lower right where the textView should become empty and go back to the original height and scrollEnabled becomes false. The middle image shows what happens when I click the button. When I start typing the textview moves down as you see in the last image on the right. I want to be able to click the button and eliminate the behavior shown on the middle image, how can I fix this?

enter image description here

import UIKit

class ViewController: UIViewController, UITextViewDelegate {

@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var messageTextView: UITextView!
@IBOutlet weak var parent: UIView!
let messageTextViewMaxHeight: CGFloat = 100


override func viewDidLoad() {
    super.viewDidLoad()

    self.messageTextView.delegate = self
}

@IBAction func Reset(sender: AnyObject) {
    messageTextView.text = ""
    messageTextView.frame.size.height = messageTextView.contentSize.height
    messageTextView.scrollEnabled = false
    self.parent.layoutIfNeeded()
}

func textViewDidChange(textView: UITextView) {
    if textView.frame.size.height >= self.messageTextViewMaxHeight {

        textView.scrollEnabled = true

    } else {
        textView.scrollEnabled = false
        textView.frame.size.height = textView.contentSize.height
    }
}

}

You can replicate my issue by following these steps in the github project: 1. keep typing words and pressing enters until you start seeing the scroll 2. Click the button you will see that the textview goes up in the blue container. This is the issue I want to eliminate!

Marissamarist answered 20/7, 2016 at 1:37 Comment(4)
see this for textview scrolling , You need to give content size #37152994Edisonedit
The textView scrolling is working fine. I want to be able to click the send button and eliminate the behavior shown on the middle image.Marissamarist
"It works perfectly until I click the "send" button on the lower right" Show the code that the "send" button triggers.Hemotherapy
It is in the code the IBAction is wired to the buttonMarissamarist
A
2

Try bellow code :-

@IBAction func Reset(sender: AnyObject) {
        messageTextView.scrollEnabled = false
        messageTextView.text = ""
        messageTextView.frame.size.height = messageTextView.contentSize.height
        parent.frame.size.height = 20

    self.view.layoutIfNeeded()
}

func textViewDidChange(textView: UITextView) {
    if textView.contentSize.height >= self.messageTextViewMaxHeight {

        textView.scrollEnabled = true

    } else {

        textView.frame.size.height = textView.contentSize.height
        textView.scrollEnabled = false
    }
}
Airdrie answered 28/7, 2016 at 9:38 Comment(1)
I downloaded your project. When I type in the textView until it reach the MaxHeight and the TextView starts scrolling. Then I click the button I still see the same behavior. The textView changed to the correct size but not in the correct position the same like the images I attached with the question.Marissamarist
P
1

Your issue is that the UITextView has conflicting properties:

  • Place on the screen
  • Size

The size being constrained will cause an issue when you need a resizable TextView. Also, when the TextView is resized, its location is being changed in this case.

Alternate method to approach the issue:

Try setting constraints to its location in relation to the bottom of the screen. When the Keyboard appears, you should move the TextView up with it. Also setting constraints on the height of a resizable TextView is bad practice unless you are planning on forcing the user to scroll.

Hope this helps.

Phanerogam answered 22/7, 2016 at 19:43 Comment(9)
I changed the TextView constraint to be only in relation to the bottom, but I still see the same behavior. I added a link to the github project in the question. BTW I want the user to be able to scroll the textView after reaching the height of a 100, that is way I have a height constraint.Marissamarist
Hey @user3126427, I tried it out myself and it worked like a charm. If you have it embedded into another project and it is not working there, try recreating the View Controller and setting it up a similar way to make sure you have not missed anything.Phanerogam
stuff like this happens all the time for me in my projects where constraints get jumbled up in a window, but recreating the window the same way might filter out something you did wrong before. Personally, I do not know how something is going wrong for you being as I myseld tested it and it worked just how you aimed for it to work.Phanerogam
By the way, I would hard-code your textview size (to the size it is initialized with) in the Reset() methodPhanerogam
there seems to be misunderstanding. Replicate my issue by following these steps in the github project: 1. keep typing words and pressing enters until you start seeing the scroll 2. Click the button you will see that the textview goes up in the blue container. This is the issue I want to eliminate!Marissamarist
I have been punching at it, but I am stuck where you are, please give me timePhanerogam
Ok, so I think I might have narrowed the issue down @Marissamarist . So, in summary, you need to recreate the frame of the UITextView (essentially manually resetting its position). This is because, in your code, you resize the frame (which adjusts its position every time you do so). So, my approach to this was storing the initial CGRect frame settings of the UITextView into NSUserDefaults and calling upon them when you call Reset(). This does not work, however, because you cannot use autolayout AND manual layout, you can use one or the other. Avoiding this requires setting up UI in OnCreatePhanerogam
Hey man, I mean I have tried everything haha. I tried recreating the project myself and same issue. I'm sorry but I do not know what to tell you at this point.Phanerogam
Also, you do not need to give me the bounty because I didnt solve your problem but I would appreciate an upvote so I can get a little credit. I really have been trying to solve your problem.Phanerogam
L
0

If you are using auto layout, you should be updating to constraint instead of updating the textView.frame.Try create a IBOutlet for your textView heightConstraint then set the updated height to it.

IBOutlet weak var textViewHeightConstraint: NSLayoutConstraint!

//calculate the height and update the constant
textViewHeightConstraint.constant = textView.contentSize.height
Listing answered 23/7, 2016 at 9:51 Comment(1)
Check my github linkMarissamarist

© 2022 - 2024 — McMap. All rights reserved.