ios 如何在 UIScrollView (Obj-C) 中禁用垂直滚动

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

How to disable vertical scrolling in UIScrollView (Obj-C)

objective-ciosuiscrollview

提问by user123

I want to disable vertical scrolling from my UIScrollView if possible.. My code is like below.. Working fine except users can scroll up and down which shouldn't be there I believe.. Thanks in advance..

如果可能的话,我想从我的 UIScrollView 禁用垂直滚动。

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)];   
    scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height); 
    scroll.pagingEnabled = YES;
    scroll.backgroundColor = [UIColor blackColor];
    int xVal = 30;

    NSInteger numberOfViews = 5;
    for (int i = 0; i < numberOfViews; i++) {
        UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)];
        UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)];
        UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)];

        testLabel2.backgroundColor = [UIColor clearColor];
        testLabel2.text =@"Test1";
        testLabel2.textColor = [UIColor whiteColor];
        testLabel2.font = [UIFont boldSystemFontOfSize:12];

        testLabel1.backgroundColor = [UIColor clearColor];
        testLabel1.text =@"Test2";
        testLabel1.textColor = [UIColor whiteColor];
        testLabel1.font = [UIFont boldSystemFontOfSize:12];

        testLabel3.backgroundColor = [UIColor clearColor];
        testLabel3.text =@"Test3";
        testLabel3.textColor = [UIColor whiteColor];
        testLabel3.font = [UIFont boldSystemFontOfSize:12];

        xVal += 120;

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)];
        view.backgroundColor = [UIColor blackColor];

        xVal += 200;

        [scroll addSubview:testLabel1];
        [scroll addSubview:testLabel2];
        [scroll addSubview:testLabel3];
        [scroll addSubview:view];
    }

    [self.view addSubview:scroll];

回答by Gavy

In my situation, I was unable to get the height of the scrollview (due to autolayout, I wasn't able to get the height in viewDidLoad). You can add this to the delegate method.

在我的情况下,我无法获得滚动视图的高度(由于自动布局,我无法在 viewDidLoad 中获得高度)。您可以将其添加到委托方法中。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}

回答by touti

you must set your scrollview content height to the scroll view height

您必须将滚动视图内容高度设置为滚动视图高度

CGSize scrollableSize = CGSizeMake(scrollableWidth, yourScrollViewHeight);
[myScrollView setContentSize:scrollableSize];

回答by Nayan

Here may be a possible duplicate

这可能是一个可能的重复

disabling vertical scrolling in UIScrollView

在 UIScrollView 中禁用垂直滚动

or you can also try this:

或者你也可以试试这个:

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

回答by Nayan Chauhan

Assuming it's an iPhone app, so the screen resolution is 320×480.

假设它是一个iPhone应用程序,所以屏幕分辨率为320×480

Now you are setting your scroll view's height as self.view.frame.size.height / 3. Here your view's height is actually taken as 460and not 480(20px for staus bar).

现在您将滚动视图的高度设置为self.view.frame.size.height / 3. 在这里,您的视图高度实际上是460而不是480(状态栏为 20px)。

So when you add the other view as subview to your scroll view, its frame goes out of the scroll's content view. So you need to manage this while setting your frames/content size.

因此,当您将另一个视图作为子视图添加到滚动视图时,其框架会超出滚动的内容视图。因此,您需要在设置框架/内容大小时进行管理。

Let me know if this works for you.

让我知道这是否适合您。

回答by AJS

There is no problem with that simply change the contentSize of your UIScrollView and you are done.Increase its width size and its height should be as it is at present.Moreover you can also hide the vertical scrollers also.

只需更改 UIScrollView 的 contentSize 就没有问题,您就完成了。增加它的宽度大小和它的高度应该是目前的样子。此外,您还可以隐藏垂直滚动条。

scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height); 

回答by Paradise

You should do like this:

你应该这样做:

aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);

回答by IOS developer

In your xml file there are two properties are available for scrollview are horizontal scroll and vertical scroll. as per your requirement you can check or uncheck and if you want to stop vertical or horizontal scroll then you have to make same content size of scrollview with height or width of scrollview respectively

在您的 xml 文件中,滚动视图有两个可用的属性:水平滚动和垂直滚动。根据您的要求,您可以选中或取消选中,如果您想停止垂直或水平滚动,则必须分别使滚动视图的内容大小与滚动视图的高度或宽度相同