如何使 uiscrollview 仅用于 ios 的垂直滚动?

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

How to make uiscrollview only vertical scrolling for ios?

iosobjective-cios8uiscrollviewautolayout

提问by Alexey Zakevich

I'm trying to make layoutinside scrollviewusing this one tutorial link

我正在尝试使用这个教程链接layout进入内部scrollview

And get the following result link

并得到以下结果链接

It will be appreciated for any advices or tutorial links. It needs only vertical scrolling

任何建议或教程链接将不胜感激。它只需要垂直滚动

回答by Jen Jose

I am sure there must be other ways to do this but a quick fix is :

我相信一定有其他方法可以做到这一点,但快速解决方法是:

1.) Create a width constraint on ContentView in Storyborad.

1.) 在 Storyborad 的 ContentView 上创建宽度约束。

2.) IBOutlet that widthContraint and set its value to the view frame width in viewDidLoad.

2.) IBOutlet 表示widthContraint 并将其值设置为viewDidLoad 中的视图框架宽度。

Suppose the name of the constraint outlet is contentViewWidthContraint.

假设约束出口的名称是 contentViewWidthContraint。

contentViewWidthContraint.constant = self.view.bounds.size.width;


Another alternative to do so from Storyboard, is to fix the Contentview width to the view's width from the storyboard or to the Scrollview, if Scrollview already has a Equal width contraint with superview . Add the "Equal Width" contraint from Contentview to either self.view or to Scrollview (if scrollview, already has the width contraint)

从 Storyboard 执行此操作的另一种替代方法是将 Contentview 宽度固定为 Storyboard 或 Scrollview 的视图宽度,如果 Scrollview 已经具有与 superview 的等宽约束。将 Contentview 中的“等宽”约束添加到 self.view 或 Scrollview(如果滚动视图已经具有宽度约束)

回答by Jesuslg123

Have you set up the "ContentView" width to match with the scroll view width? I had the same problem and I fixed with "Equal Widths".

您是否设置了“ContentView”宽度以匹配滚动视图宽度?我遇到了同样的问题,我用“等宽”解决了这个问题。

"Equal Widths" will tell to your "ContentView" to use the same width of the "Scroll View", which should be fitting the screen if you have set up the constrain properly.

Equal Widths”将告诉您的“ ContentView”使用与“ Scroll View”相同的宽度,如果您正确设置了约束,它应该适合屏幕。

You can do this easily on the storyboard.

您可以在情节提要上轻松完成此操作。

  1. Drag and drop, with right click (important!!!), from "ContentView" to "ScrollView"
  2. Release the click, you will be prompted with a menu, select "Equal Widths".

    Prompted menu

  1. 拖放,右键单击(重要!!!),从“ContentView”到“ScrollView”
  2. 放开点击,会出现一个菜单提示,选择“等宽”。

    提示菜单

This should fix your problem using the scrollview with AutoLayout from Storyboard editor.

这应该可以解决您在 Storyboard 编辑器中使用带有 AutoLayout 的滚动视图的问题。

You can find a full tutorial how to use ScrollView with Autolayout and Storyboard here.

您可以在此处找到如何将 ScrollView 与 Autolayout 和 Storyboard 结合使用的完整教程。

I hope this is useful for you :)

我希望这对你有用:)

回答by MCS

In the Storyboard set the width of the elements contained in your UIScrollView equal to the width of this UIScrollView (by selecting all elements and the UIScrollView holding in the panel on the left of your Storyboard and then setting the 'Equal Widths' constraint under 'Pin' on the bottom of your Storyboard). Just pinning the right sides of the elements to that of the UIScrollView won't work as it will adjust the size of its "display view" to the width of the largest element and if this is smaller than the width of the UIScrollView all elements will just appear aligned to its left side.

在故事板中,将 UIScrollView 中包含的元素的宽度设置为等于该 UIScrollView 的宽度(通过选择故事板左侧面板中的所有元素和 UIScrollView,然后在“Pin”下设置“Equal Widths”约束' 在故事板的底部)。只是将元素的右侧固定到 UIScrollView 的右侧是行不通的,因为它会将其“显示视图”的大小调整为最大元素的宽度,如果这小于 UIScrollView 的宽度,则所有元素都将只是看起来与它的左侧对齐。

回答by Markus

There is also another possibility that offers a very good result.

还有另一种可能会提供非常好的结果。

You can mark a checkbox:

您可以标记一个复选框:

enter image description here

在此处输入图片说明

O programmatically:

O 以编程方式:

scrollView.alwaysBounceVertical = true

回答by Vineeth Joseph

For disabling the horizontal scroll, you can set the content size in the -(void)scrollViewDidScrollmethod.

要禁用水平滚动,您可以在-(void)scrollViewDidScroll方法中设置内容大小。

[self.scrollView setContentOffset: CGPointMake(0, self.scrollView.contentOffset.y)];
self.scrollView.directionalLockEnabled = YES;

回答by iYoung

Try to set it's width to 0 & height equal to content size like this:

尝试将其宽度设置为 0 和高度等于内容大小,如下所示:

self.scrollView.contentSize = CGSizeMake(0, self.scrollView.contentSize.height);

This will work as you want. Try it & tell if still facing any issue.

这将如您所愿。尝试并判断是否仍然面临任何问题。

回答by Jatin Patel - JP

Too set UIScrollViewconstraints as like below code so it will occupied whole screen.Not exceed the screen size.

UIScrollView像下面的代码那样设置约束,所以它会占据整个屏幕。不超过屏幕尺寸。

Leading Space = 0 from mainView
Top Space = 0 from mainView
Bottom Space = 0 from mainView 
Trailing Space = 0 from mainView

回答by M Abdul Sami

You need to set the width of UIScrollView equal to or less than the width of your Parent View. Two ways to do it:

您需要将 UIScrollView 的宽度设置为等于或小于您的父视图的宽度。有两种方法可以做到:

1) You can do this in Storyboard via layout constraints

1) 您可以通过布局约束在 Storyboard 中执行此操作

2) You can do this programatically:

2)您可以以编程方式执行此操作:

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.contentSize.height);

回答by Vitalii

This is because scroll view have no idea where your content should end.

这是因为滚动视图不知道您的内容应该在哪里结束。

But when at least one item inside your scroll view has its "trailing space" constraint attached to a view outside the scroll view (usually a view the scroll view is sitting in or some other view of a higher level, which "knows" its width) - the scroll view will automatically get an idea about your wanted width and won't scroll horizontally (unless that trailing constraint implies having your content outside the screen).

但是,当滚动视图中的至少一个项目将其“尾随空间”约束附加到滚动视图外部的视图时(通常是滚动视图所在的视图或更高级别的某个其他视图,它“知道”其宽度) - 滚动视图将自动了解您想要的宽度并且不会水平滚动(除非尾部约束意味着您的内容在屏幕之外)。

Better if all items inside scroll view have their "trailing space" constraints connected either to each other or to a view outside the scroll view. But not the scroll view itself.

如果滚动视图内的所有项目都将它们的“尾随空间”约束相互连接或连接到滚动视图外部的视图,则更好。但不是滚动视图本身。

No additional code or extra constraints needed for this to work.

无需额外的代码或额外的约束即可工作。