固定屏幕(不是内容)自动布局和滚动型宽 [英] Autolayout and Scrollview width fixed to screen (not content)

查看:179
本文介绍了固定屏幕(不是内容)自动布局和滚动型宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照与(其中大部分是相同的),例如exmaples的号码作为的https:/ /www.youtube.com/watch?v=UnQsFlMGDsI

I have followed and number of exmaples (most of which are the same) such as https://www.youtube.com/watch?v=UnQsFlMGDsI

我opena新项目时,在(顶部,底部,左,右),则内容视图约束到滚动视图的内部拖放滚动视图,然后是内容的观点在于,约束滚动视图到它的上海华和那么它的宽和高的滚动视图上海华。滚动型红色和内容视图黑色。

I opena new project, drop a scrollview in and then a "content" view in that, constrain the scrollview to it's superview (top, bottom, left and right) then the Content view is constrained to the inside of the scrollview and then it's width and hight to the scrollview superview. Scrollview in red and content view black.

我滴在一个文本框,然后将其约束到顶部,领导和内容视图的结尾。这一切看起来好故事板,但是当我运行它的内容的看法是不存在的,文本框是它了。

I drop a Textfield in and then constraint it to the top, leading and trailing of the content view. It all looks good in storyboard but when I run it the content view is not there and the textfield is the width of the objects in it (which are based on the textfield) in it.

无固定内容视图高度

我然后更改内容视图HIGHT是一个固定的800,现在我可以看到内容的看法,但宽度由文本框(而不是其他方式),即大小,如果我添加更多的测试信息定文字这一切变得更宽。

I then change the content view hight to be a fixed 800 and I can now see the content view but width is fixed by the size of the textfield (not the other way around), i.e. if I add more "Test info" text it all gets wider.

在这里输入的形象描述

这是不是我真正的项目,但我只是想消除的原因所以没有这个从无到有,它确实是相同的。我看不出什么,我缺少的(一个希望勾选框)。

This is not my real project but I just wanted to eliminate reasons so did this from scratch and it does the same. I can not see what I am missing (hoping a tick box).

如果我强迫的文本框为700范围内的IT水平滚动离开了,我看不出针脚的内容视图宽屏幕大小。

If I force the textfield to be 700 wide it scrolls left horizontally, I can not see how to pin the content view width to the screen size.

推荐答案

已更新答案

很抱歉,我previously公布答案。我错过了一些点。在这里,我会解释给你一步一步有什么需要做的(见截图)。

Sorry about the answer that I previously posted. I missed some points in that. Here I will explain to you step by step what need to be done(refer screenshots).

下面是制约整个视图的概述说:

Here is an overview of the entire view with constraints added:

在这里输入的形象描述

首先要做的,添加滚动视图,并添加约束为前置,顶部和底部相对于上海华。

First thing to do, add the scrollview and add the constraint for leading, trailing, top and bottom with respect to superview.

在这里输入的形象描述

接下来要做的,滚动视图中添加的UIView 。现在,这部分是棘手的,你只需要添加作为滚动视图的内容视图的视图。所以,你需要添加到滚动视图的任何意见应真正去这个视图中。对这一观点,设置的限制等,顶部,底部,前和后。但棘手的部分在这里,低优先级(250)设置的底部约束上海华(即滚动视图)为零。同时设定这一观点的宽度等于滚动视图的上海华,那是的ViewController的看法,以及滚动视图。

Next thing to do, add a UIView inside the scroll view. Now this part is tricky, the view you just added need to act as the content view of the scroll view. So whatever views you need to add to the scroll view should actually go inside this view. For this view, set the constraints like, top, bottom, leading and trailing. But the tricky part here is, set the bottom constraint to superview(ie, the scrollview) as zero with low priority(250). Also set the width of this view as equal to the scroll view's superview, that is the ViewController's view as well as the scroll view.

在这里输入的形象描述

有关内容视图约束

在这里输入的形象描述

底部约束设置为零低优先级。

Bottom constraint set to zero low priority.

在这里输入的形象描述

参照滚动型的上海华相等宽度以及滚动视图。

Equal width with reference to scrollview's superview as well as the scroll view.

由于内部意见的约束需要当谈到滚动工作的缘故视图的底部约束被赋予较高的优先级底部约束设置为低优先级。

The bottom constraint is set to low priority since the constraints of the inner views need to be given higher priority when it comes to bottom constraint of the view for the sake of scrolling to work.

现在添加所需的视图内容视图里面,你的情况的文本字段。添加所需的所有约束相对于内容视图(顶部,前置,底部)。添加任意数量的要添加子视图,但要确保您指定的最下方查看关于内容视图底部的约束,太需要有优先权。
下面是我的内容视图中添加文本域的约束:

Now add the required view inside the content view, in your case the text field. Add all the required constraints with respect to the content view(top, leading, trailing, bottom). Add any number of subviews you wish to add, but ensure you specify the bottom constraint for the bottom most view with respect to the content view, that too with required priority. Here are the constraints for the textfield that I added inside the content view:

在这里输入的形象描述

请注意,在这种情况下,底部约束是必需的优先

Note that the bottom constraint in this case is required priority.

最终输出的:

在这里输入的形象描述

黄色=的ViewController的看法,绿色=滚动视图,红色=容器视图,粉色=文本字段。

Yellow = ViewController's view, Green = scroll view, Red = container view, pink = text field.

的人谁需要做一个横向滚动视图,只需切换内容视图的宽度与高度及其与尾部底部约束约束的音符。也就是说,设定的SuperView和滚动型和针后间隔相等的高度,以滚动查看低优先级的。

A note for anyone who need to make a horizontal scrollview, just switch the width of the content view with height and its bottom constraint with trailing constraint. That is, set equal heights with superview and scrollview and pin trailing space to scroll view with low priority.

这篇关于固定屏幕(不是内容)自动布局和滚动型宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆