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!
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!
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
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
In the Interface Builder,
This worked for me
textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;
Go to Interface Builder
:
view controller
that contains your text view
Adjusts Scroll View Insets
propertyI 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?
On swift (Xcode 6)
self.automaticallyAdjustsScrollViewInsets = false
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)
What you have to understand is that this relates to the navigation bar:
I tried the following trick, it worked.
- (void)viewDidLoad{
self.textView.scrollEnabled = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.textView.scrollEnabled = YES;
}
This is an unprofessional solution, I'm sure... But just putting a blank label behind the textview solves the problem.
This just worked for me (available from iOS 7)
[<#your UITextViewInstance#> setTextContainerInset:UIEdgeInsetsZero];