Blank space at top of UITextView in iOS 10

邮差的信 提交于 2019-12-27 11:00:39

问题


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!


回答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



回答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




回答3:


In the Interface Builder,

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



回答4:


This worked for me

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



回答5:


Go to Interface Builder:

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




回答6:


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?




回答7:


On swift (Xcode 6)

 self.automaticallyAdjustsScrollViewInsets = false



回答8:


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

yourTextView.textContainerInset = UIEdgeInsetsZero;



回答9:


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)



回答10:


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.



回答11:


I tried the following trick, it worked.

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



回答12:


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




回答13:


This just worked for me (available from iOS 7)

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



回答14:


Solution for Swift 4:

textView.textContainerInset = UIEdgeInsets.zero


来源:https://stackoverflow.com/questions/18931934/blank-space-at-top-of-uitextview-in-ios-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!