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!

user3126427
  • 855
  • 2
  • 14
  • 29
  • see this for textview scrolling , You need to give content size http://stackoverflow.com/questions/37152494/uitextview-scrollenabled-yes-not-working-after-set-scrollenabled-no-in-ios8/37210762#37210762 – Prashant Tukadiya Jul 20 '16 at 04:43
  • 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. – user3126427 Jul 20 '16 at 18:44
  • "It works perfectly until I click the "send" button on the lower right" Show the code that the "send" button triggers. – matt Jul 22 '16 at 19:26
  • It is in the code the IBAction is wired to the button – user3126427 Jul 22 '16 at 19:33

3 Answers3

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
    }
}
Mitul Marsoniya
  • 5,272
  • 4
  • 33
  • 57
  • 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. – user3126427 Jul 28 '16 at 17:42
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.

Ryan Cocuzzo
  • 3,109
  • 7
  • 35
  • 64
  • 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. – user3126427 Jul 23 '16 at 17:47
  • 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. – Ryan Cocuzzo Jul 23 '16 at 20:20
  • 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. – Ryan Cocuzzo Jul 23 '16 at 20:22
  • By the way, I would hard-code your textview size (to the size it is initialized with) in the Reset() method – Ryan Cocuzzo Jul 23 '16 at 20:24
  • 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! – user3126427 Jul 23 '16 at 21:21
  • I have been punching at it, but I am stuck where you are, please give me time – Ryan Cocuzzo Jul 23 '16 at 23:13
  • Ok, so I think I might have narrowed the issue down @user3126427 . 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 `OnCreate` – Ryan Cocuzzo Jul 23 '16 at 23:22
  • 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. – Ryan Cocuzzo Jul 26 '16 at 02:43
  • 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. – Ryan Cocuzzo Jul 26 '16 at 02:45
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
Zac Kwan
  • 5,587
  • 4
  • 20
  • 27