UIScrollView不滚动iOS

时间:2013-12-19 10:54:03

标签: ios uiscrollview

我正在尝试在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方法,它不是吗?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

试试这个,

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循环条件部分。 希望这会对你有所帮助。