By Ali Sufyan


2013-09-21 11:18:27 8 Comments

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!

14 comments

@Kostas Tsoleridis 2018-10-19 13:00:09

Solution for Swift 4:

textView.textContainerInset = UIEdgeInsets.zero

@Dumoko 2013-10-31 12:26:29

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

@hufeng03 2015-03-20 04:24:52

Make change in storyboard maybe sometimes is better. Go to "Identity Inspector" -> "User Defined Runtime Attributes", and add a definition there.

@Cœur 2017-06-29 13:26:00

automaticallyAdjustsScrollViewInsets is deprecated. From iOS11, use contentInsetAdjustmentBehavior

@casillas 2017-06-23 21:10:30

I tried the following trick, it worked.

- (void)viewDidLoad{
   self.textView.scrollEnabled = NO;  
}       
- (void)viewDidAppear:(BOOL)animated {
   [super viewDidAppear:animated];
   self.textView.scrollEnabled = YES;
}

@jrturton 2013-09-21 11:51:37

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

@Stephen Melvin 2013-10-18 01:23:07

This is a better answer because it addresses not only how to correct the issue, but why it happens in the first place.

@leftspin 2013-10-22 05:02:39

BTW, you don't have to subclass and override. Just set automaticallyAdjustsScrollViewInsets to NO after you instantiate the UITextView.

@jrturton 2013-10-22 06:25:40

@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 2014-01-16 08:45:43

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 ?

@Craig 2014-06-12 13:57:47

@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 2014-06-12 15:12:49

@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 2014-06-12 15:19:27

@jrturton thanks for elaborating, I appreciate it. When you said method I wasn't considering a properties getter. Great tip.

@Gellie Ann 2016-01-15 00:12:36

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? :)

@jrturton 2016-01-15 07:07:36

@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.

@mbm29414 2016-12-02 14:05:59

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!

@Cœur 2017-06-29 13:27:49

automaticallyAdjustsScrollViewInsets is deprecated. From iOS11, use contentInsetAdjustmentBehavior

@Alex 2019-03-11 15:36:00

What if i don't want the same behavior on two textView which are in the same ViewController ?

@CodeStage 2016-01-28 20:15:12

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.

@Aamir 2016-01-10 07:06:48

Go to Interface Builder:

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

enter image description here

@CodeOverRide 2016-03-04 22:46:19

this was what I was looking for. also you can do this by adding 'self.automaticallyAdjustsScrollViewInsets = false' to viewController

@UpSampler 2017-02-14 17:36:01

This is a good and straigtforward answer, that just happened to end my search for "why is this text not showing".

@lberyouk 2015-02-28 22:02:44

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

yourTextView.textContainerInset = UIEdgeInsetsZero;

@Saqib Omer 2015-08-04 12:30:13

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)

@InitJason 2017-02-14 01:21:27

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];

@Ashok 2017-11-10 06:35:33

Nothing worked for me except this. Swift, Xcode 9.1.

@Ravi Bihani 2015-04-17 05:49:49

This worked for me

textView.textContainerInset = UIEdgeInsetsZero;  
textView.textContainer.lineFragmentPadding = 0;

@Claudio Barrozo Prado 2015-02-26 18:16:40

On swift (Xcode 6)

 self.automaticallyAdjustsScrollViewInsets = false

@Suragch 2015-12-30 09:40:43

Do this in viewDidLoad of the View Controller with the text view.

@Cœur 2017-06-29 13:28:14

automaticallyAdjustsScrollViewInsets is deprecated. From iOS11, use contentInsetAdjustmentBehavior

@RyanTCB 2015-01-09 00:04:04

This just worked for me (available from iOS 7)

[<#your UITextViewInstance#> setTextContainerInset:UIEdgeInsetsZero];

@Terence 2014-09-03 18:20:31

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

@Ilker Baltaci 2015-03-17 13:56:22

this is not a solution =)

@user2268026 2013-10-13 21:21:48

In the Interface Builder,

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

@platinor 2015-07-28 19:00:30

This is the easiest solution for my case in ios8

@Maik639 2015-10-19 21:01:40

Note: It's a property of UIViewController. Couldn't find it and then realized I didn't read the text completely

@MonsieurDart 2013-09-27 08:05:46

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?

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] How to lose margin/padding in UITextView?

49 Answered Questions

[SOLVED] Vertically align text to top within a UILabel

62 Answered Questions

[SOLVED] Placeholder in UITextView

11 Answered Questions

[SOLVED] UITextView is not scrolled to top when loaded

19 Answered Questions

[SOLVED] UITextView starts at Bottom or Middle of the text

15 Answered Questions

[SOLVED] UITextView that expands to text using auto layout

1 Answered Questions

UITextView leaving space on top

3 Answered Questions

[SOLVED] UITextView Text alignmnet at Top

1 Answered Questions

UITextview - top space gets masked - Settings in XCode

1 Answered Questions

Sponsored Content