xcode 如何使 ScrollView 宽度 = 设备屏幕宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31388129/
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-09-15 07:28:10  来源:igfitidea点击:

How to make ScrollView width = to device screen width?

iosiphonexcodeswiftuiscrollview

提问by vk2015

I am making an app that has a (child) View inside a Scroll View. I understand I have to explicitly set the child View's width since the Scroll View won't provide that information to the child, but how can I make this child View's width auto-adjust based on the width of the device?

我正在制作一个在滚动视图中具有(子)视图的应用程序。我知道我必须明确设置子视图的宽度,因为滚动视图不会向子视图提供该信息,但是如何根据设备的宽度使这个子视图的宽度自动调整?

For example, in the below I've set the width to 380 which centers the "Quotes" in iPhone 6, but makes them slightly right of center in iPhone 5.

例如,在下面我将宽度设置为 380,它在 iPhone 6 中将“引号”居中,但在 iPhone 5 中使它们稍微偏右。

screenshot

截屏

回答by rmp

You can use auto layout to set the width of your subView equal to your main View by performing a control drag from your subView to your main View like this:

您可以使用自动布局通过执行从子视图到主视图的控件拖动来设置子视图的宽度等于主视图,如下所示:

enter image description here

在此处输入图片说明

Then select "Equal Widths" from the popup

然后从弹出窗口中选择“等宽”

enter image description here

在此处输入图片说明

回答by user3069397

Select viewController

选择视图控制器

Stretch the view you want full screen size to the size of the screen

将想要全屏大小的视图拉伸到屏幕大小

Click the central icon from the three choices along the bottom of the window

单击窗口底部三个选项中的中央图标

You will an icon pop up

你会弹出一个图标

The top 4 values will have a dotted red line joining them

前 4 个值将有一条红色虚线连接它们

click them all

全部点击

at the bottom of the pop up you will see 'add 4 constraints'

在弹出窗口的底部,您将看到“添加 4 个约束”

click this

点击这个

Done!

完毕!

回答by knutigro

You can delete the constraint for width. The view will place it self horizontally becuase of the leading and trailing space to superview. No need for a specific width.

您可以删除宽度的约束。由于要超级视图的前导和尾随空间,视图将自身水平放置。不需要特定的宽度。

I think your scrollview will be scrolling vertically so in that case you probably need to provide a constraint for the height or your view, and update scrollview contentsize accordingly to this.

我认为您的滚动视图将垂直滚动,因此在这种情况下,您可能需要为高度或视图提供约束,并相应地更新滚动视图的内容大小。

回答by Andriy Gordiychuk

Since your child view is located in the view hierarchy of your view controller's view you can assign constraints between your child view and your view controller's view.

由于您的子视图位于视图控制器视图的视图层次结构中,因此您可以在子视图和视图控制器视图之间分配约束。

Ctrl drag from your child view to your view controller's view. You will see option to set them to have equal widths. Select it. Then adjust the constant if you want the width of your child view to be lower.

Ctrl 从您的子视图拖动到您的视图控制器的视图。您将看到将它们设置为具有相等宽度的选项。选择它。如果您希望子视图的宽度较低,则调整常量。

You can then add leading and trailing constraints between your child view and your scroll view.

然后,您可以在子视图和滚动视图之间添加前导和尾随约束。

Doing it in this way will also ensure that you set the content width of you scroll view dynamically and correctly.

这样做还可以确保您动态且正确地设置滚动视图的内容宽度。

Let me know if it works for you

请让我知道这对你有没有用