Blank space at top of UITextView in iOS 10

42,022

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:

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

enter image description here

Share:
42,022

Related videos on Youtube

Ali Sufyan
Author by

Ali Sufyan

CEO at GuruTrax Solutions.

Updated on July 08, 2022

Comments

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

    I didn't set any contentOffSet. Please Help!

  • Stephen Melvin
    Stephen Melvin over 10 years
    This is a better answer because it addresses not only how to correct the issue, but why it happens in the first place.
  • leftspin
    leftspin over 10 years
    BTW, you don't have to subclass and override. Just set automaticallyAdjustsScrollViewInsets to NO after you instantiate the UITextView.
  • jrturton
    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
    Paul Brewczynski over 10 years
    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
    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
    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
    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
    Ilker Baltaci about 9 years
    this is not a solution =)
  • hufeng03
    hufeng03 about 9 years
    Make change in storyboard maybe sometimes is better. Go to "Identity Inspector" -> "User Defined Runtime Attributes", and add a definition there.
  • platinor
    platinor almost 9 years
    This is the easiest solution for my case in ios8
  • Admin
    Admin over 8 years
    Note: It's a property of UIViewController. Couldn't find it and then realized I didn't read the text completely
  • Suragch
    Suragch over 8 years
    Do this in viewDidLoad of the View Controller with the text view.
  • Gellie Ann
    Gellie Ann over 8 years
    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
    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
    CodeOverRide about 8 years
    this was what I was looking for. also you can do this by adding 'self.automaticallyAdjustsScrollViewInsets = false' to viewController
  • mbm29414
    mbm29414 over 7 years
    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!
  • InitJason
    InitJason about 7 years
    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];
  • UpSampler
    UpSampler about 7 years
    This is a good and straigtforward answer, that just happened to end my search for "why is this text not showing".
  • Cœur
    Cœur almost 7 years
    automaticallyAdjustsScrollViewInsets is deprecated. From iOS11, use contentInsetAdjustmentBehavior
  • Cœur
    Cœur almost 7 years
    automaticallyAdjustsScrollViewInsets is deprecated. From iOS11, use contentInsetAdjustmentBehavior
  • Cœur
    Cœur almost 7 years
    automaticallyAdjustsScrollViewInsets is deprecated. From iOS11, use contentInsetAdjustmentBehavior
  • Ashok
    Ashok over 6 years
    Nothing worked for me except this. Swift, Xcode 9.1.
  • Alex
    Alex about 5 years
    What if i don't want the same behavior on two textView which are in the same ViewController ?