Blank space at top of UITextView in iOS 10
Solution 1
A text view is a scroll view. View controllers will add a content offset automatically to scroll views, as it is assumed they will want to scroll up behind the nav bar and status bar.
To prevent this, set the following property on the view controller containing the text view:
self.automaticallyAdjustsScrollViewInsets = NO
Solution 2
The current answer that IronManGill gave is not a good solution, because it is not based on an understanding of why the problem happens in the first place.
jrturton's answer is also not the cleanest way to solve it. You don't need to override. Keep it simple!
All you need is to set the following:
self.automaticallyAdjustsScrollViewInsets = NO;
in the viewDidLoad method.
Check out the docs: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621372-automaticallyadjustsscrollviewin
Solution 3
In the Interface Builder,
- Select the view controller which contains the UITextView.
- Go to the attribute inspector.
- Uncheck "Adjust Scroll View Insets."
Solution 4
This worked for me
textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;
Solution 5
Go to Interface Builder
:
- Select
view controller
that contains yourtext view
- uncheck
Adjusts Scroll View Insets
property
Related videos on Youtube
Comments
-
Ali Sufyan almost 2 years
I have UITextView with some text in it. Everything was fine with iOS 6 but now with iOS 7 it leaves the blank space on top and then place the text below the middle of the textview.
I didn't set any contentOffSet. Please Help!
-
Stephen Melvin over 10 yearsThis is a better answer because it addresses not only how to correct the issue, but why it happens in the first place.
-
leftspin over 10 yearsBTW, you don't have to subclass and override. Just set automaticallyAdjustsScrollViewInsets to NO after you instantiate the UITextView.
-
jrturton over 10 years@leftspin its a pretty rare case where you haven't subclassed a view controller. And overriding the method means your code will still work on lower iOS versions - setting the property means you need a compatibility check first.
-
Paul Brewczynski over 10 yearsSo if the my Model View Controller is outside UINavigationController the offset would be cleared ? It is kind of crazy... This behavior is ONLY correct when my textview is aligned to the top, otherwise it is rubbish... Could anybody explain why Apple done something like that ?
-
Craig almost 10 years@jrturton apologies for this being an older post and please excuse me as I am new to objective-c but this is currently a property of UIViewController...did it used to be a method?
-
jrturton almost 10 years@Craig No, it's always been a property, but if you override the getter method then you don't have to check if you're running an older version of iOS before calling it.
-
Craig almost 10 years@jrturton thanks for elaborating, I appreciate it. When you said method I wasn't considering a properties getter. Great tip.
-
Ilker Baltaci about 9 yearsthis is not a solution =)
-
hufeng03 about 9 yearsMake change in storyboard maybe sometimes is better. Go to "Identity Inspector" -> "User Defined Runtime Attributes", and add a definition there.
-
platinor almost 9 yearsThis is the easiest solution for my case in ios8
-
Admin over 8 yearsNote: It's a property of UIViewController. Couldn't find it and then realized I didn't read the text completely
-
Suragch over 8 yearsDo this in
viewDidLoad
of the View Controller with the text view. -
Gellie Ann over 8 yearsI don't know what's wrong with me but I find this: "self.automaticallyAdjustsScrollViewInsets = NO;" easier to understand than this: "set the property automaticallyAdjustsScrollViewInsets on the view controller containing the text view to NO.". I know they are the same, but...or is it because of the itch to copy and paste? :)
-
jrturton over 8 years@GelFermis you're right. The answer originally had some stuff about overriding the property (to make it compatible pre ios7) then it got edited, I've rewritten it.
-
CodeOverRide about 8 yearsthis was what I was looking for. also you can do this by adding 'self.automaticallyAdjustsScrollViewInsets = false' to viewController
-
mbm29414 over 7 yearsHad this problem after embedding a
UIViewController
in a navigation controller in a storyboard. Unchecking "Adjust Scroll View Insets" in IB fixed this issue. Thanks for pointing me in the right direction! -
InitJason about 7 yearsThis was the best solution keeping the insets and scrolling under translucent bar correctly. I would suggest this modification.
[self.textView setContentOffset:CGPointMake(0.f, -self.topLayoutGuide.length) animated:NO];
-
UpSampler about 7 yearsThis is a good and straigtforward answer, that just happened to end my search for "why is this text not showing".
-
Cœur almost 7 years
automaticallyAdjustsScrollViewInsets
is deprecated. From iOS11, usecontentInsetAdjustmentBehavior
-
Cœur almost 7 years
automaticallyAdjustsScrollViewInsets
is deprecated. From iOS11, usecontentInsetAdjustmentBehavior
-
Cœur almost 7 years
automaticallyAdjustsScrollViewInsets
is deprecated. From iOS11, usecontentInsetAdjustmentBehavior
-
Ashok over 6 yearsNothing worked for me except this. Swift, Xcode 9.1.
-
Alex about 5 yearsWhat if i don't want the same behavior on two textView which are in the same ViewController ?