117

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.enter image description here

I didn't set any contentOffSet. Please Help!

Ali Sufyan
  • 2,038
  • 3
  • 17
  • 27

14 Answers14

217

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
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • 7
    This is a better answer because it addresses not only how to correct the issue, but why it happens in the first place. – Stephen Melvin Oct 18 '13 at 01:23
  • 1
    BTW, you don't have to subclass and override. Just set automaticallyAdjustsScrollViewInsets to NO after you instantiate the UITextView. – leftspin Oct 22 '13 at 05:02
  • 1
    @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. – jrturton Oct 22 '13 at 06:25
  • 1
    So 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 ? – Paweł Brewczynski Jan 16 '14 at 08:45
  • @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? – Craig Jun 12 '14 at 13:57
  • @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. – jrturton Jun 12 '14 at 15:12
  • @jrturton thanks for elaborating, I appreciate it. When you said method I wasn't considering a properties getter. Great tip. – Craig Jun 12 '14 at 15:19
  • I 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? :) – Gellie Ann Jan 15 '16 at 00:12
  • @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. – jrturton Jan 15 '16 at 07:07
  • Had 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! – mbm29414 Dec 02 '16 at 14:05
  • `automaticallyAdjustsScrollViewInsets ` is deprecated. From iOS11, use [`contentInsetAdjustmentBehavior`](https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior) – Cœur Jun 29 '17 at 13:27
  • What if i don't want the same behavior on two textView which are in the same ViewController ? – Alex Mar 11 '19 at 15:36
58

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dumoko
  • 2,614
  • 3
  • 25
  • 34
  • 1
    Make change in storyboard maybe sometimes is better. Go to "Identity Inspector" -> "User Defined Runtime Attributes", and add a definition there. – hufeng03 Mar 20 '15 at 04:24
  • `automaticallyAdjustsScrollViewInsets ` is deprecated. From iOS11, use [`contentInsetAdjustmentBehavior`](https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior) – Cœur Jun 29 '17 at 13:26
46

In the Interface Builder,

  • Select the view controller which contains the UITextView.
  • Go to the attribute inspector.
  • Uncheck "Adjust Scroll View Insets."
user2268026
  • 577
  • 3
  • 3
37

This worked for me

textView.textContainerInset = UIEdgeInsetsZero;  
textView.textContainer.lineFragmentPadding = 0;
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
Ravi Bihani
  • 471
  • 4
  • 5
24

Go to Interface Builder:

  1. Select view controller that contains your text view
  2. uncheck Adjusts Scroll View Insets property

enter image description here

Aamir
  • 16,329
  • 10
  • 59
  • 65
  • this was what I was looking for. also you can do this by adding 'self.automaticallyAdjustsScrollViewInsets = false' to viewController – CodeOverRide Mar 04 '16 at 22:46
  • This is a good and straigtforward answer, that just happened to end my search for "why is this text not showing". – UpSampler Feb 14 '17 at 17:36
10

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:

enter image description here

In this case, the inset is not added:

enter image description here

This really seem strange to me… Why would the OS look at the view's deepness to decide whether it should extend the view?

MonsieurDart
  • 6,005
  • 2
  • 43
  • 45
7

You can delete the blank space on top of your UITextView by adding:

yourTextView.textContainerInset = UIEdgeInsetsZero;
Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
lberyouk
  • 119
  • 1
  • 4
6

On swift (Xcode 6)

 self.automaticallyAdjustsScrollViewInsets = false
  • Do this in `viewDidLoad` of the View Controller with the text view. – Suragch Dec 30 '15 at 09:40
  • `automaticallyAdjustsScrollViewInsets ` is deprecated. From iOS11, use [`contentInsetAdjustmentBehavior`](https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior) – Cœur Jun 29 '17 at 13:28
5

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)
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
  • This 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];` – InitJason Feb 14 '17 at 01:21
  • 1
    Nothing worked for me except this. Swift, Xcode 9.1. – Ashok Nov 10 '17 at 06:35
1

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.
CodeStage
  • 540
  • 4
  • 6
1

I tried the following trick, it worked.

- (void)viewDidLoad{
   self.textView.scrollEnabled = NO;  
}       
- (void)viewDidAppear:(BOOL)animated {
   [super viewDidAppear:animated];
   self.textView.scrollEnabled = YES;
}
casillas
  • 16,351
  • 19
  • 115
  • 215
1

Solution for Swift 4:

textView.textContainerInset = UIEdgeInsets.zero
0

This is an unprofessional solution, I'm sure... But just putting a blank label behind the textview solves the problem.

Terence
  • 11
  • 2
0

This just worked for me (available from iOS 7)

[<#your UITextViewInstance#> setTextContainerInset:UIEdgeInsetsZero];
RyanTCB
  • 7,400
  • 5
  • 42
  • 62