Jason90
Jason90

Reputation: 105

How to change UIView height based on elements inside it

I am creating a chat interface and have a UITextview and button for sending message inside a UIView. The height of UITextView changes based on its content size but UIView height does not change with it. Chat view

I will appreciate any help on this.

Here are constraints on the message field Message field constraints

Upvotes: 3

Views: 20400

Answers (3)

pajevic
pajevic

Reputation: 4657

Set autolayout constraints specifying a fixed space and pin them from your UIView top to the UITextView top and from your UIView bottom to the UITextView bottom.

It is unclear from your question how you are setting the height of your container view and your text view. You should have a height constraint on the text view and change its constant when you want to resize the text view. Do not change the frame directly. The container view must not have a height constraint - it should resize solely because of the bottom and top constraints to the text view.

Upvotes: 2

Bhadresh Kathiriya
Bhadresh Kathiriya

Reputation: 3244

without auto layout code:

txtView.frame = CGRectMake(view.frame.origin.x + 8, view.frame.origin.y + 8, view.frame.size.width - 100, view.frame.size.height - 16);

btnSend.frame = CGRectMake(view.frame.size.width - 92, (view.frame.size.height/2) - 25, 84, 50);

Upvotes: 1

Pranav Wadhwa
Pranav Wadhwa

Reputation: 7736

If you programmatically created the view:

containerView.frame.size.height = textView.frame.size.height

If you create the view in the storyboard

Create a height constraint for your view. Then connect that constraint to your code. Then run this:

heightConstraint.constant = textView.frame.size.height

Upvotes: 6

Related Questions