Blank space at top of UITextView in iOS 10
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Extra blank space at the top of a UITextView in iOS 10 usually comes from one of two places: the text container’s internal inset or the scroll view’s adjusted content inset. UITextView is both a text container and a scroll view, so several spacing layers can combine and make the top gap look mysterious. The fix is to inspect those layers separately instead of guessing.
Start with the Text Container Insets
UITextView has default internal padding controlled by textContainerInset and lineFragmentPadding. If you want the text to sit flush with the top and sides, reduce those values explicitly.
This removes the built-in top, bottom, left, and right text padding from the text container itself.
If you only care about the top spacing, you can target that side specifically:
Check Scroll View Insets Too
Because UITextView inherits from UIScrollView, top space can also come from contentInset. That is separate from the text container inset.
If your text view is inside a view controller that automatically adjusts scroll view insets, the controller may be adding space for bars or layout guides.
In iOS 10-era code, a common controller-level fix was:
That setting affects scroll views managed by the controller, including text views.
A Practical Combined Fix
If you want a quick reset to remove most accidental top whitespace, apply all three of these:
This addresses both the text-layout padding and the scroll-view adjustment layer.
Why Fonts and Attributed Text Can Make It Look Worse
Custom fonts, paragraph styles, and attributed text can make the blank area more noticeable even when the root cause is still inset-related. For example, a large line height or paragraph spacing before the first line can exaggerate the appearance of "top padding."
That is why you should inspect the attributed text too if zeroing the insets does not fully solve the issue.
Keep the Difference Clear
It helps to separate the spacing sources mentally:
- '
textContainerInset: padding inside the text container' - '
lineFragmentPadding: left and right text padding inside each line fragment' - '
contentInset: scroll view content padding' - controller automatic inset adjustment: extra space added by container behavior
When developers treat them as one thing, the debugging process becomes much harder than it needs to be.
Common Pitfalls
One common mistake is changing only contentInset when the visible gap is actually caused by textContainerInset.
Another mistake is forgetting that UITextView is a scroll view. Controller-level inset adjustment can introduce spacing even if the text container is configured correctly.
Developers also sometimes remove one spacing layer and then stop, even though a second one is still active. The result improves but does not fully disappear.
Finally, if the text uses custom paragraph styles, do not assume every top gap is a UIKit bug. Sometimes the attributed content is legitimately adding space before the first line.
Summary
- Top blank space in
UITextViewusually comes fromtextContainerInset,contentInset, or automatic scroll inset adjustment. - Set
textContainerInsetandlineFragmentPaddingexplicitly when you want tight text layout. - Reset
contentInsetif the text view behaves like a padded scroll view. - In iOS 10 view-controller code, automatic scroll inset adjustment can also be part of the problem.
- Debug the text container and scroll view layers separately to find the real source of the gap.

