UIScrollView不滚动iOS

Posted

技术标签:

【中文标题】UIScrollView不滚动iOS【英文标题】:UIScrollView not scrolling iOS 【发布时间】:2013-12-19 10:54:03 【问题描述】:

我正在尝试在 ios 中水平滚动的单个视图中创建多个 UIScrollView。到目前为止,这是我的代码:

-(void)updateSection 
    [feedLoadingActInd stopAnimating];
    feedLoadingActInd.hidden = YES;
    builder = [NSMutableArray array];
    float xPosition = 0;
    float xPosBut = 0;

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)];
    [scrollView setScrollEnabled:YES];
    scrollView.backgroundColor = [UIColor yellowColor];

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;

    for (int i = 0; i < itemArticleArray.count; i++) 
        testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosBut, 40, 40, 40)];
        [testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [testButton setTitle:@"Test" forState:UIControlStateNormal];
        [testButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
        testButton.backgroundColor = [UIColor blueColor];
        xPosBut += testButton.frame.size.width;
        NSLog(@"scroll.frame.size.width = %f", scrollView.frame.size.width);

        xPosition += 2;
        UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 350, scrollView.frame.size.height - 8)];
        seperatorView.backgroundColor = [UIColor redColor];
        [scrollView addSubview:seperatorView];
        xPosition += 350;

        [seperatorView addSubview:testButton];
        [scrollView addSubview:seperatorView];
        [builder addObject:testButton];

    
    [self addSubview:scrollView];
    [scrollView setUserInteractionEnabled:YES];
    [scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];

    NSLog(@"scroll.contentsize.width = %f", scrollView.contentSize.width);

但是,没有一个滚动视图实际上是滚动的,我对此感到困惑,因为添加了多个按钮。此外,我添加的按钮在我按下它们时实际上并没有做任何事情。他们应该运行 buttonPressed 方法,但它没有?

任何帮助将不胜感激!

【问题讨论】:

您将滚动视图内容大小设置为等于框架,因此组件不会滚动。为了滚动,scroll.contentSize.y > scroll.frame.height [scrollView setContentSize: CGSizeMake(xPosition, 700)];或者你通过生成的按钮高度来调整你的高度 犯了一个小错误,我需要它水平滚动而不是垂直滚动 我已经编辑了上面的帖子和代码? 【参考方案1】:

试试这个,

float xPosition = 0;
    float xPosBut = 0;

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)];
    [scrollView setScrollEnabled:YES];

    scrollView.backgroundColor = [UIColor yellowColor];

    for (int i = 0; i < 10; i++) 
       UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 10, scrollView.frame.size.width, 50)];
        [testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [testButton setTitle:@"Test" forState:UIControlStateNormal];
        [testButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
        testButton.backgroundColor = [UIColor blueColor];
        xPosition += testButton.frame.size.width;
        NSLog(@"xposbut = %f", xPosBut);
        NSLog(@"scroll.frame.size.width = %f", scrollView.frame.size.width);

        xPosition += scrollView.frame.size.width+2;
        UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 2, scrollView.frame.size.height - 8)];
        seperatorView.backgroundColor = [UIColor redColor];
        [scrollView addSubview:seperatorView];
        xPosition +=scrollView.frame.size.width+ 4;

        [self.view addSubview:scrollView];
        [scrollView addSubview:seperatorView];
        [scrollView addSubview:testButton];
        [builder addObject:testButton];

    
    [scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
    [self.view addSubview:scrollView];

根据您的要求设置 for 循环条件部分。 希望这会对你有所帮助。

【讨论】:

如果我将scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)]; 的代码行更改为:scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)]; 那么它不允许我滚动或按下按钮?

以上是关于UIScrollView不滚动iOS的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView 不滚动

尽管提供了代表,uiscrollview 不滚动

为啥显示键盘时 UIScrollView 不滚动?

UIScrollView 在 customView 中不滚动

触摸某些元素时 UIScrollView 不滚动

为啥我在 UIScrollView 中的 UIView 不滚动?