UITextView that expands to text using auto layout
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A UITextView that grows to fit its content is a common requirement in iOS apps — chat bubbles, comment fields, and form inputs all need this behavior. The key is disabling scrolling (isScrollEnabled = false), which tells the text view to use its intrinsic content size instead of a fixed scrollable frame. Combined with Auto Layout constraints that do not fix the height, the text view automatically expands as the user types. For dynamic height in table or collection view cells, you also need to invalidate the cell layout when the text changes.
Basic Self-Sizing UITextView
When isScrollEnabled is false, UITextView reports an intrinsic content size equal to its text content. Auto Layout uses this to determine the height automatically.
Setting a Maximum Height
To prevent the text view from growing beyond a maximum height, add a height constraint and re-enable scrolling when the content exceeds it:
When text exceeds maxHeight, scrolling is enabled and the text view stops growing. When text shrinks below the threshold, it resizes back down.
Using UITextViewDelegate for Dynamic Updates
Self-Sizing Table View Cells
For expanding text views inside UITableViewCell:
Set tableView.rowHeight = UITableView.automaticDimension and tableView.estimatedRowHeight = 60 for automatic cell sizing.
SwiftUI Equivalent
In SwiftUI, TextEditor automatically sizes with the .fixedSize(horizontal:vertical:) modifier:
In iOS 16+, TextField also supports multi-line input with the axis: .vertical parameter:
Common Pitfalls
- Forgetting to set
isScrollEnabled = false: This is the single most important step. When scrolling is enabled (the default),UITextViewuses a fixed frame and scrolls its content instead of expanding. The intrinsic content size is not reported to Auto Layout. - Adding a fixed height constraint: A fixed-height constraint (
heightAnchor.constraint(equalToConstant: 100)) prevents the text view from expanding. UsegreaterThanOrEqualToConstantfor a minimum height, or omit the height constraint entirely. - Not calling
invalidateIntrinsicContentSize()in custom subclasses: When you override properties liketextorattributedTextin aUITextViewsubclass, callinvalidateIntrinsicContentSize()to trigger Auto Layout to recalculate the height. - Table view cells not resizing dynamically: When text changes in a cell, the table view does not automatically recalculate row heights. Call
tableView.beginUpdates()/tableView.endUpdates()intextViewDidChange(_:)to trigger a height recalculation without reloading the cell. - Text view jumping when toggling
isScrollEnabled: SwitchingisScrollEnabledbetween true and false can cause the text view to scroll to the top. SetcontentOffset = .zeroor preserve the scroll position when toggling to avoid a visual jump.
Summary
- Set
isScrollEnabled = falseto makeUITextViewreport its intrinsic content size to Auto Layout - Do not add a fixed height constraint — use
greaterThanOrEqualToConstantfor a minimum height or omit height entirely - For a maximum height, switch
isScrollEnabledback totrueand cap the height when content exceeds the limit - In table view cells, use
UITableView.automaticDimensionfor row height and callbeginUpdates()/endUpdates()when text changes - In SwiftUI, use
TextEditorwith.fixedSize(horizontal: false, vertical: true)orTextFieldwithaxis: .vertical
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.