iOS中如何锁定scrollView的水平滚动
我有一个滚动视图。我希望它只向一个方向滚动(垂直)。有没有办法可以锁定水平滚动...? ...
i have a scrollView. i want it to scroll only in one direction(VERTICAL). Is there a way in which i can lock the horizontal scrolling...?? ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
与您的答案相同(改编):
禁用UIScrollView 中的垂直滚动
正确,这里的所有答案都可以...
但是如果由于某种奇怪的原因您的 contentSize 在两个维度上都应该高于
UIScrollView
,您可以禁用水平滚动实现UIScrollView
协议方法-(void)scrollViewDidScroll:(UIScrollView *)aScrollView
只需将其添加到您的
UIViewController
中,它只是用户调用时调用的方法滚动您的
UIScrollView
,这样做可以强制它的内容始终具有相同的 .x
Same (adapted) answer for you as in:
Disabling vertical scrolling in UIScrollView
right, all answers here are ok...
but if for some strange reason your contentSize should be higher than the
UIScrollView
in both dimensions, you can disable the horizontal scrolling implementing theUIScrollView
protocol method-(void)scrollViewDidScroll:(UIScrollView *)aScrollView
just add this in your
UIViewController
it's just the method called when the user scroll your
UIScrollView
, and doing so you force the content of it to have always the same .x
应该有帮助。我只是通过执行类似的技术水平锁定了我的滚动视图。
should help. I just locked my scroll view horizontally, by doing the similar technique.
我发现限制滚动视图移动的最安全、最成功的方法是子类化 UIScrollView 并重写 setContentOffset:animated: 和 setContentOffset: 方法(代码如下)。
重写这些方法的优点是,它可以在任何 UIKit 代码开始对其进行操作之前直接更改请求的 contentOffset,从而避免修改 中的 contentOffset 时可能发生的任何副作用scrollViewDidScroll:或其他UIScrollViewDelegate方法。
创建新的子类只需几分钟,而且效果非常好。
The safest and most successful method I've found to constrain the movement of a scroll view is to subclass UIScrollView and override setContentOffset:animated: and setContentOffset: methods (code below).
The advantage of overriding these methods is that it directly alters the requested contentOffset before any of the UIKit code starts to act on it, avoiding any of the side effects that can occur when modifying the contentOffset in scrollViewDidScroll: or other UIScrollViewDelegate methods.
It takes just a few minutes to create the new subclass, and it works like a charm.
请参阅这个问题,您必须将可滚动大小设置为视图的相应大小,以便没有任何内容可以滚动。
See this question, you have to set the scrollable size to the corresponding size of the view so that there is nothing to scroll.
如果 UIScrollView 的
contentSize
属性与 UIScrollView 的实际宽度相同,则不可能进行水平滚动。当水平滚动有效时,您想停止水平滚动吗? (即
contentSize
比实际的 UIScrollView 宽度更宽?)如果“是”,你为什么要这样做?If the UIScrollView's
contentSize
property has the same as the actual width of the UIScrollView, then no horizontal scrolling will be possible.Are you wanting to stop horizontal scrolling when horizontal scrolling would be valid? (i.e. the
contentSize
is wider than the actual UIScrollView width?) If 'yes', why are you wanting to do that?你可以试试这个,
这里1450是我的视野高度。
You can try this,
here 1450 is my view height.
如果将 contentSize 宽度设置为大于滚动视图框架,则滚动视图只能水平滚动。如果将 contentSize 宽度设置为与滚动视图相同,它将不会滚动。例如
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,100,100)]
[scrollView setContentSize:CGSizeMake(100, 300)]
The scrollView is only scrollable horizontally if you set the contentSize width bigger than the scrollView frame. If you set the contentSize width to the same as the scrollview it won't scroll. For example
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,100,100)]
[scrollView setContentSize:CGSizeMake(100, 300)]
哦,因为 iOS7 使用这个(在 initWithNibName 中):
并创建包含 3 个页面的页面滚动条
oh since iOS7 use this (in initWithNibName):
and create the page scroller with 3 pages
就我而言,x 和 y 的 contentSize 高于 UIScrollView。对我来说,meronixe 的答案非常好,但对于我的需求来说还不够完整。不幸的是,水平滚动条仍然可见。
我可以用下面的代码来改变这一点
In my case the contentSize was higher than the UIScrollView for x and y. For me meronixe's answer worked out quite well, but was for my needs not quite complete. Unfortunately the horizontal scrollbar was still visible.
This I could change with the following code in
Swift
使
contentOffset.x = 0
禁用水平滚动此外,您可以禁用滚动指示器
Swift
Make
contentOffset.x = 0
for disable horizontal scrollAdditionally you can disable scroll indicator as
另一个解决方案:
您可以将对齐顶部位置设置为 -200 或任何您想要的值。
another solution :
You can set the align top position to -200 or whatever value you want.
水平或垂直滚动锁定
您可以将垂直滚动的 x 更改为 y。
确保像这样添加
UIScrollViewDelegate
:并设置 ScrollView 的委托
Horizontal or Vertical Scroll Lock
You can change the x to y for vertical scrolling.
Make sure to add
UIScrollViewDelegate
like this:And set the delegate for the ScrollView