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

use*_*123 7 objective-c uiscrollview ios

如果可能,我想从我的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];
Run Code Online (Sandbox Code Playgroud)

Gav*_*avy 21

在我的情况下,我无法获得scrollview的高度(由于autolayout,我无法在viewDidLoad中获得高度).您可以将其添加到委托方法中.

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


tou*_*uti 10

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

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