alionthego
alionthego

Reputation: 9773

how to dynamically change textView's superview height as textView's height increases

I have a textView at the bottom of my screen which is a subView of a containerView. This is the text input section of a chat app. I would like to know how to increase the size of the textView's container view as the text view size increases. I am using autolayout. I have used the following textView delegate method to resize the textView and it seems to work fine. But the container view does not change size regardless of what I try. Thank you.

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    messageTextView.frame.size.height = messageTextView.contentSize.height
    return true
}

Upvotes: 0

Views: 1655

Answers (3)

Chitra
Chitra

Reputation: 46

Just in case someone still looking for the answer, I solved my issue by setting my textView's Layout property to Autoresizing Mask in size inspector

enter image description here

Upvotes: 0

alionthego
alionthego

Reputation: 9773

Okay. I solved this by simply not putting the textView inside another view. I still have the two views in the same position but they are both just subview's of the main view. Neither one is inside the other. Then changing the height of each view by the contentSize.height of the textView works fine.

Upvotes: 1

Daniel Krom
Daniel Krom

Reputation: 10068

I have the same func that works for me:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    var height = self.noteValueLabel!.contentSize;
    textView.sizeToFit();

        super.frame.size.height+=textView.frame.maxY - super.frame.height + 3.0;
    }

it works for me, when noteValueLabel is the UITextView and super.frame.size is yours messageTextView

Upvotes: 0

Related Questions