【问题标题】:How to disable vertical scrolling in UIScrollView (Obj-C)如何在 UIScrollView (Obj-C) 中禁用垂直滚动
【发布时间】:2012-08-04 09:13:23
【问题描述】:

如果可能的话,我想从我的 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];

【问题讨论】:

  • 是的,我用过同样的东西,但对我没用!!
  • @NayanChauhan 它被标记为 ios。
  • 我把你的代码放在一个新项目的 viewDidLoad 中,我根本没有滚动...尝试在这个方法的末尾设置 scrollView contentSize。
  • 只需设置contentSize。相信我,它有效。它不起作用的唯一原因是如果你像这样设置 contentSize CGSizeMake(yourDesiredWidth , 0 ) ; //which is correct 然后将它的 contentSize 设置为高度大于 scrollView 的框架高度。

标签: objective-c ios uiscrollview


【解决方案1】:

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

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

【讨论】:

  • 为我工作,谢谢。
  • 在 iOS 8.1.1 上完美运行,在 TableViewCell 中有一个滚动视图!谢谢!
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    这里可能有重复

    disabling vertical scrolling in UIScrollView

    或者你也可以试试这个:

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

    【讨论】:

      【解决方案4】:

      假设它是一个 iPhone 应用,那么屏幕分辨率为 320×480

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

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

      让我知道这是否适合你。

      【讨论】:

        【解决方案5】:

        没有问题,只要改变你的 UIScrollView 的 contentSize 就可以了。增加它的宽度大小,它的高度应该和现在一样。此外,你也可以隐藏垂直滚动条。

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

        【讨论】:

          【解决方案6】:

          你应该这样做:

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

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-07-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多