Blank space at top of UITextView in iOS 10
IosIphoneXcodeCocoa TouchUitextfieldIos Problem Overview
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!
Ios Solutions
Solution 1 - Ios
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 - Ios
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 - Ios
In the Interface Builder,
- Select the view controller which contains the UITextView.
- Go to the attribute inspector.
- Uncheck "Adjust Scroll View Insets."
Solution 4 - Ios
This worked for me
textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;
Solution 5 - Ios
Go to Interface Builder
:
-
Select
view controller
that contains yourtext view
-
uncheck
Adjusts Scroll View Insets
property
Solution 6 - Ios
I really wonder if this is a real feature… This 64 point automatic inset is only added when the UIScrollView
or UITextView
as the greater deepness of all subviews of the view controller's view. For example, if you add a view behind (here I'm talking about z-buffer, not view imbrication) the scroll view, this 64 point inset is not automatically added.
For example, this one adds the inset:
In this case, the inset is not added:
This really seem strange to me… Why would the OS look at the view's deepness to decide whether it should extend the view?
Solution 7 - Ios
You can delete the blank space on top of your UITextView
by adding:
yourTextView.textContainerInset = UIEdgeInsetsZero;
Solution 8 - Ios
On swift (Xcode 6)
self.automaticallyAdjustsScrollViewInsets = false
Solution 9 - Ios
I resolved this issue by setting content offset to a negative value. Here is Swift code.
yourTextView.setContentOffset(CGPoint(x: 0, y: -150), animated: true)
Solution 10 - Ios
What you have to understand is that this relates to the navigation bar:
- If your content goes under the navigation bar, you want the view controller to automatically adjust the scroll view insets (default).
- If your content does not go under the navigation bar (you probably used the top layout guide for the top space constraint), you can disable the automatic scroll view inset adjustment like mentioned in most of the other replies.
Solution 11 - Ios
I tried the following trick, it worked.
- (void)viewDidLoad{
self.textView.scrollEnabled = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.textView.scrollEnabled = YES;
}
Solution 12 - Ios
Solution for Swift 4:
textView.textContainerInset = UIEdgeInsets.zero
Solution 13 - Ios
This is an unprofessional solution, I'm sure... But just putting a blank label behind the textview solves the problem.
Solution 14 - Ios
This just worked for me (available from iOS 7)
[<#your UITextViewInstance#> setTextContainerInset:UIEdgeInsetsZero];