简体   繁体   English

带有Autolayout iOS的scrollView内的UITextView不起作用

[英]UITextView inside a scrollView with Autolayout iOS not working

I'm trying to create a dynamic UIView that holds some fixed graphics elements and some dynamic text. 我正在尝试创建一个动态UIView,其中包含一些固定的图形元素和一些动态文本。 I can't find a tutorial, or something in documentation that works for my project. 我找不到适用于我的项目的教程或文档中的某些内容。 Below is my exploded view (my view with scrollView and main view's height is forced to 800 in iphone 5 view, only for the pourpose of this question) 下面是我的分解视图(仅出于此问题的目的,我的scrollView和主视图的高度在iphone 5视图中被强制为800) 在此处输入图片说明

What do I want to acheive? 我要达到什么目的? The Header,the image,static view and static view 2 are all fixed, and they will not change with their contents. 页眉,图像,静态视图和静态视图2都是固定的,并且不会随其内容改变。 Label 1, Label 2 and the textarea will grow with their content size. 标签1,标签2和文本区域将随着其内容大小而增长。

在此处输入图片说明

I have worked on this thing for about a day, and I need a little help because I can't achive any results. 我已经做了一天的研究,并且需要一点帮助,因为我无法获得任何结果。 I have tried this solution: 我已经尝试过此解决方案:

solution 1 解决方案1

With this solution I can see in NSLog() the textview constraint that return a correct value 通过这种解决方案,我可以在NSLog()中看到返回正确值的textview约束。

self.textViewHeightConstraint.constant = [textview sizeThatFits:CGSizeMake(textview.frame.size.width, CGFLOAT_MAX)].height; 

but the textarea frame at runtime remains the same. 但运行时的textarea框架保持不变。

i have tried also this tutorial: 我也尝试过本教程:

solution 2 解决方案2

It works for me in a project with only a scrollview, content view and textview. 它在只有滚动视图,内容视图和文本视图的项目中对我有用。 When i try to use that logic in my project it doesn't work anymore. 当我尝试在项目中使用该逻辑时,它将不再起作用。

I have tried many other solutions, so my questions are: How I can achive the desired result? 我已经尝试了许多其他解决方案,所以我的问题是:如何获得理想的结果? Or maybe there's something ridiculously wrong in my layout setting? 还是在我的布局设置中有一个荒谬的错误? Or simply, is there someone that can point my to a tutorial that cover a similar view organization? 还是简单地说,有人可以指出涉及相似视图组织的教程吗?

You should avoid having a complex nested subviews inside a UIScrollView with AutoLayout, you should instead put them inside a subView. 您应该避免在具有AutoLayout的UIScrollView包含复杂的嵌套子视图,而应将其放在subView中。 The best thing to start with: 最好的开始:

1: Place a single UIView inside your UIScrollView with leading / trailing / top / bottom constraints set to 0 to the UIScrollView . 1:将单个UIView你的内部UIScrollViewleading / trailing / top / bottom设置为0以约束UIScrollView

2: Place all the subviews inside the one single UIView , 2:将所有子视图放在一个 UIView

3: Once you change the text/dynamic content of any of the subViews inside the UIView , you just calculate the scrollView.contentSize and set it accordingly by code needed for a set of complex nested subviews. 3:更改UIView任何子视图的文本/动态内容后,您只需计算scrollView.contentSize并通过一组复杂的嵌套子视图所需的代码进行相应的设置。

Your better option: 您更好的选择:

Work with a UITableView instead, it has the scrollView methods and it handles the dynamic layouts for you very easy without all the spaghetti code. 而是使用UITableView ,它具有scrollView方法,并且无需所有意大利面条代码即可非常轻松地为您处理动态布局。

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

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