如何根据其中的元素更改 UIView 高度 [英] How to change UIView height based on elements inside it
问题描述
我正在创建一个聊天界面,并有一个 UITextview 和用于在 UIView 内发送消息的按钮.UITextView 的高度根据其内容大小而变化,但 UIView 高度不会随之变化.聊天视图
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
我将不胜感激.
这里是对消息字段的约束消息字段约束
Here are constraints on the message field Message field constraints
推荐答案
设置自动布局约束,指定一个固定空间,并将它们从 UIView
顶部固定到 UITextView
顶部和从你的 UIView 底部到 UITextView 底部.
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.
这篇关于如何根据其中的元素更改 UIView 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!