简体   繁体   English

UIScrollView不滚动iOS

[英]UIScrollView not scrolling iOS

I am trying to create several UIScrollViews in a single view that scroll horizontally in iOS. 我试图在单个视图中创建多个UIScrollViews,这些视图在iOS中水平滚动。 This is my code so far: 到目前为止,这是我的代码:

-(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);

} }

However, none of the scroll views are actually scrolling, which I am confused about, as there are multiple buttons being added. 但是,没有滚动视图实际在滚动,我对此感到困惑,因为添加了多个按钮。 Also, the buttons that I have added, are not actually doing anything when I press them. 另外,我添加的按钮在按下时实际上并没有执行任何操作。 They should be running the buttonPressed method, and it doesn't? 他们应该在运行buttonPressed方法,不是吗?

Any help would be muchly appreciated! 任何帮助将不胜感激!

try this, 尝试这个,

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];

set the for loop condition part as per your requirement. 根据您的要求设置for循环条件部分。 hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM