简体   繁体   English

如何使用AutoLayout在UIScrollView中设置UITextView

[英]How to set a UITextView in a UIScrollView with AutoLayout

Forgive me if this has already been answered elsewhere, but I couldn't find a thread about this: 如果这已经在其他地方得到了解答,请原谅我,但是我找不到关于此的话题:

I have a VC with the following hierarchy: 我有一个具有以下层次结构的VC:

在此处输入图片说明

I want my ContentView elements to scroll within my ScrollView. 我希望ContentView元素在ScrollView中滚动。 I have a lot of definitions in the textView, and some are longer than others. 我在textView中有很多定义,有些定义长于其他。 Previously, only the longer definitions would scroll and the shorter ones wouldn't, but since I had an auto layout conflict, the links weren't able to be clicked at the end. 以前,只有较长的定义会滚动,较短的定义则不会滚动,但是由于我遇到了自动布局冲突,因此最后无法单击链接。 I have the ScrollView constrained to the View and the ContentView constrained to the ScrollView. 我将ScrollView限制为View,将ContentView限制为ScrollView。 I also have the ContentView constrained equalWidth and equalHeight from the main View. 我还从主视图中获得了ContentView约束的equalWidth和equalHeight。 In order for this solution to work, I had to set the height of the ContentView to something like 1000 pts so that longer definitions wouldn't be cut off. 为了使该解决方案有效,我不得不将ContentView的高度设置为1000 pts,这样就不会截断更长的定义。 The only problem is that now all the definitions are scrolling (even ones that don't need to - that don't have enough text to go over the screen). 唯一的问题是,现在所有的定义都在滚动(甚至不需要滚动-没有足够的文本显示在屏幕上)。 And the user can scroll down and see enough white space to cover the screen, depending on how brief the definition is. 用户可以向下滚动并看到足够的空白区域来覆盖屏幕,具体取决于定义的简短程度。

My question is: how can I arrange the constraints so that longer definitions scroll when needed and shorter ones do not - how can I set the constraint dynamically? 我的问题是:如何排列约束,以便较长的定义在需要时滚动而较短的定义则不需要-如何动态设置约束?

Thanks 谢谢

If I am understanding you correctly, it sounds like you are having issues with setting your scrollView's contents so that it will scroll when it is supposed, and not scroll when it shouldn't. 如果我正确地理解了您的声音,听起来您在设置scrollView的内容时遇到了问题,以便它可以在应有的状态下滚动,而在不应该的情况下不滚动。 Correct? 正确? Then (assuming your constraints are set properly) your scrollView's contentSize should be set by autoLayout based on the size of your Content View. 然后(假设您的约束设置正确),应该根据内容视图的大小通过autoLayout设置scrollView的contentSize。

Try setting a property up to dynamically control the height of the Content View. 尝试设置属性以动态控制内容视图的高度。 In your ViewController, create your property: 在您的ViewController中,创建您的属性:

@property (nonatomic, strong) IBOutlet NSLayoutConstraint *contentViewHeight;

Then, wherever you are setting the text of your labels, you should calculate the new heights, and update your constraint based on that. 然后,无论您在何处设置标签文本,都应计算新的高度,并根据此高度更新约束。 For reference, here is a question regarding how to determine the height of text in a label. 作为参考,这是一个有关如何确定标签中文本高度的问题。

Once you have the height you can set it like this: 达到高度后,即可按以下方式进行设置:

self.contentViewHieight.constant = // Updated Height
[self.view setNeedsUpdateConstraints];

I hope that helps. 希望对您有所帮助。 In my own experience, I have been limiting my use of UIScrollView since the way it has been updated to use AutoLayout. 以我自己的经验,自从将UIScrollView更新为使用AutoLayout的方式以来,我一直在限制使用UIScrollView。 I mostly use UITableView & UICollectionView in its place. 我主要使用UITableView和UICollectionView代替它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM