iOS中如何锁定scrollView的水平滚动

发布于 2024-10-21 09:39:24 字数 54 浏览 8 评论 0原文

我有一个滚动视图。我希望它只向一个方向滚动(垂直)。有没有办法可以锁定水平滚动...? ...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

野侃 2024-10-28 09:39:24

与您的答案相同(改编):

禁用UIScrollView 中的垂直滚动

正确,这里的所有答案都可以...

但是如果由于某种奇怪的原因您的 contentSize 在两个维度上都应该高于 UIScrollView ,您可以禁用水平滚动实现UIScrollView 协议方法 -(void)scrollViewDidScroll:(UIScrollView *)aScrollView

只需将其添加到您的 UIViewController 中,

float oldX; // here or better in .h interface

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(oldX, aScrollView.contentOffset.y)];
    // or if you are sure you wanna it always on left:
    // [aScrollView setContentOffset: CGPointMake(0, aScrollView.contentOffset.y)];
}

它只是用户调用时调用的方法滚动您的 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 the UIScrollView protocol method -(void)scrollViewDidScroll:(UIScrollView *)aScrollView

just add this in your UIViewController

float oldX; // here or better in .h interface

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(oldX, aScrollView.contentOffset.y)];
    // or if you are sure you wanna it always on left:
    // [aScrollView setContentOffset: CGPointMake(0, aScrollView.contentOffset.y)];
}

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

轮廓§ 2024-10-28 09:39:24
self.scrollview.contentSize = CGSizeMake(1, self.scrollview.frame.size.height * number_of_items);

应该有帮助。我只是通过执行类似的技术水平锁定了我的滚动视图。

self.scrollview.contentSize = CGSizeMake(1, self.scrollview.frame.size.height * number_of_items);

should help. I just locked my scroll view horizontally, by doing the similar technique.

樱花细雨 2024-10-28 09:39:24

我发现限制滚动视图移动的最安全、最成功的方法是子类化 UIScrollView 并重写 setContentOffset:animated:setContentOffset: 方法(代码如下)。

重写这些方法的优点是,它可以在任何 UIKit 代码开始对其进行操作之前直接更改请求的 contentOffset,从而避免修改 中的 contentOffset 时可能发生的任何副作用scrollViewDidScroll:或其他UIScrollViewDelegate方法。

创建新的子类只需几分钟,而且效果非常好。

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
    // restrict movement to vertical only
    CGPoint newOffset = CGPointMake(0, contentOffset.y);
    [super setContentOffset:newOffset animated:animated];
}

- (void)setContentOffset:(CGPoint)contentOffset {
    // restrict movement to vertical only
    CGPoint newOffset = CGPointMake(0, contentOffset.y);
    [super setContentOffset:newOffset];    
}

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.

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
    // restrict movement to vertical only
    CGPoint newOffset = CGPointMake(0, contentOffset.y);
    [super setContentOffset:newOffset animated:animated];
}

- (void)setContentOffset:(CGPoint)contentOffset {
    // restrict movement to vertical only
    CGPoint newOffset = CGPointMake(0, contentOffset.y);
    [super setContentOffset:newOffset];    
}
中二柚 2024-10-28 09:39:24

请参阅这个问题,您必须将可滚动大小设置为视图的相应大小,以便没有任何内容可以滚动。

See this question, you have to set the scrollable size to the corresponding size of the view so that there is nothing to scroll.

银河中√捞星星 2024-10-28 09:39:24

如果 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?

π浅易 2024-10-28 09:39:24

你可以试试这个,

self.ScrollView.contentSize = CGSizeMake(self.ScrollView.frame.size.width, 1450);

这里1450是我的视野高度。

You can try this,

self.ScrollView.contentSize = CGSizeMake(self.ScrollView.frame.size.width, 1450);

here 1450 is my view height.

掀纱窥君容 2024-10-28 09:39:24

如果将 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)]

俏︾媚 2024-10-28 09:39:24

哦,因为 iOS7 使用这个(在 initWithNibName 中):

self.automaticallyAdjustsScrollViewInsets = NO;

并创建包含 3 个页面的页面滚动条

self.pageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.pageView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];
[self.pageView setShowsVerticalScrollIndicator:NO];
[self.pageView setPagingEnabled:YES];
[self.view addSubview:self.pageView];

oh since iOS7 use this (in initWithNibName):

self.automaticallyAdjustsScrollViewInsets = NO;

and create the page scroller with 3 pages

self.pageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.pageView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];
[self.pageView setShowsVerticalScrollIndicator:NO];
[self.pageView setPagingEnabled:YES];
[self.view addSubview:self.pageView];
小忆控 2024-10-28 09:39:24

就我而言,x 和 y 的 contentSize 高于 UIScrollView。对我来说,meronixe 的答案非常好,但对于我的需求来说还不够完整。不幸的是,水平滚动条仍然可见。

我可以用下面的代码来改变这一点

- (void)webViewDidFinishLoad:(UIWebView *)webView

[self.webView.scrollView setShowsHorizontalScrollIndicator:NO];

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

- (void)webViewDidFinishLoad:(UIWebView *)webView

[self.webView.scrollView setShowsHorizontalScrollIndicator:NO];
浅黛梨妆こ 2024-10-28 09:39:24

Swift

使 contentOffset.x = 0 禁用水平滚动

   extension ViewController: UIScrollViewDelegate {
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
            scrollView.contentOffset.x = 0//to lock horizontal scrolling
            //scrollView.contentOffset.y = 0//to lock vertical scrolling
        }
    }

此外,您可以禁用滚动指示器

  scrollView.showsHorizontalScrollIndicator = false//to disable HorizontalScrollIndicator
    scrollView.showsVerticalScrollIndicator = false//to disable VerticalScrollIndicator

Swift

Make contentOffset.x = 0 for disable horizontal scroll

   extension ViewController: UIScrollViewDelegate {
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
            scrollView.contentOffset.x = 0//to lock horizontal scrolling
            //scrollView.contentOffset.y = 0//to lock vertical scrolling
        }
    }

Additionally you can disable scroll indicator as

  scrollView.showsHorizontalScrollIndicator = false//to disable HorizontalScrollIndicator
    scrollView.showsVerticalScrollIndicator = false//to disable VerticalScrollIndicator
oО清风挽发oО 2024-10-28 09:39:24

另一个解决方案:
您可以将对齐顶部位置设置为 -200 或任何您想要的值。

another solution :
You can set the align top position to -200 or whatever value you want.

奢望 2024-10-28 09:39:24

水平或垂直滚动​​锁定

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
        scrollView.contentOffset.x = 0
    }
}

您可以将垂直滚动的 x 更改为 y。

确保像这样添加 UIScrollViewDelegate

class MyViewController: UIViewController, UIScrollViewDelegate {

}

并设置 ScrollView 的委托

scrollView.delegate = self

Horizontal or Vertical Scroll Lock

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
        scrollView.contentOffset.x = 0
    }
}

You can change the x to y for vertical scrolling.

Make sure to add UIScrollViewDelegate like this:

class MyViewController: UIViewController, UIScrollViewDelegate {

}

And set the delegate for the ScrollView

scrollView.delegate = self
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文