iOS 10 中 UITextView 顶部的空白区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18931934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:30:33  来源:igfitidea点击:

Blank space at top of UITextView in iOS 10

iosiphonexcodecocoa-touchuitextfield

提问by Ali Sufyan

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

我有 UITextView,里面有一些文字。iOS 6 一切正常,但现在使用 iOS 7,它会在顶部留下空白空间,然后将文本放在 textview 的中间下方。在此处输入图片说明

I didn't set any contentOffSet. Please Help!

我没有设置任何 contentOffSet。请帮忙!

回答by jrturton

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

回答by Dumoko

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.

IronManGill 给出的当前答案并不是一个好的解决方案,因为它首先不是基于对问题发生原因的理解。

jrturton's answer is also not the cleanest way to solve it. You don't need to override. Keep it simple!

jrturton 的答案也不是解决它的最干净的方法。你不需要覆盖。把事情简单化!

All you need is to set the following:

您只需要设置以下内容:

self.automaticallyAdjustsScrollViewInsets = NO;

in the viewDidLoad method.

在 viewDidLoad 方法中。

Check out the docs: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621372-automaticallyadjustsscrollviewin

查看文档:https: //developer.apple.com/documentation/uikit/uiviewcontroller/1621372-automaticallyadjustsscrollviewin

回答by user2268026

In the Interface Builder,

在界面生成器中,

  • Select the view controller which contains the UITextView.
  • Go to the attribute inspector.
  • Uncheck "Adjust Scroll View Insets."
  • 选择包含 UITextView 的视图控制器。
  • 转到属性检查器。
  • 取消选中“调整滚动视图插入”。

回答by Ravi Bihani

This worked for me

这对我有用

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

回答by Aamir

Go to Interface Builder:

前往Interface Builder

  1. Select view controllerthat contains your text view
  2. uncheck Adjusts Scroll View Insetsproperty
  1. 选择view controller包含您的text view
  2. 取消选中Adjusts Scroll View Insets属性

enter image description here

在此处输入图片说明

回答by MonsieurDart

I really wonder if this is a real feature… This 64 point automatic inset is only added when the UIScrollViewor UITextViewas 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.

我真的想知道这是否是一个真正的功能......这个 64 点自动插入仅在视图控制器视图的所有子视图的UIScrollViewUITextView更大的深度时添加。例如,如果您在滚动视图后面添加一个视图(这里我说的是 z 缓冲区,而不是视图叠瓦),则不会自动添加此 64 点插入。

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?

这对我来说真的很奇怪……为什么操作系统会查看视图的深度来决定是否应该扩展视图?

回答by Claudio Barrozo Prado

On swift (Xcode 6)

快速(Xcode 6)

 self.automaticallyAdjustsScrollViewInsets = false

回答by lberyouk

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

您可以UITextView通过添加以下内容来删除顶部的空白区域:

yourTextView.textContainerInset = UIEdgeInsetsZero;

回答by Saqib Omer

I resolved this issue by setting content offset to a negative value. Here is Swift code.

我通过将内容偏移设置为负值解决了这个问题。这是 Swift 代码。

yourTextView.setContentOffset(CGPoint(x: 0, y: -150), animated: true)

回答by CodeStage

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.
  • 如果您的内容位于导航栏下方,您希望视图控制器自动调整滚动视图插入(默认)。
  • 如果您的内容不在导航栏下方(您可能使用了顶部空间约束的顶部布局指南),您可以禁用大多数其他回复中提到的自动滚动视图插入调整。