Question

I am declaring several UIViews that are added to my UIScroll view in a for loop, I would like to know how to change the color of each UIView background when the loop progresses. Currently each UIVIew just stays the same color grey.

This is what my code looks like:

UIScrollView *htmlContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 60.0, self.view.frame.size.width, 293.0)];
    NSInteger viewcount = 4;
    color = 0;
    for(int i = 0; i< viewcount; i++) {
        color = color + 4;
        NSLog(@"%f", color);
        CGFloat y = i * self.view.frame.size.height;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];
        view.backgroundColor = [UIColor colorWithRed:(200.0 + i)/255.0 green:(200.0 + i)/255.0 blue:(200.0 + i)/255.0 alpha:1.0];;
        [htmlContainerScrollView addSubview:view];

    }
Was it helpful?

Solution

The problem is in this line:

view.backgroundColor = [UIColor colorWithRed:(200.0 + i)/255.0 green:(200.0 + i)/255.0 blue:(200.0 + i)/255.0 alpha:1.0];;

Whenever Red, Green, and Blue has the same value, the color is gray.

Add constant value different then 200 to, at least, Red component and you'll see different colors.

OTHER TIPS

The problem is that your adding "i" instead of "color" when you try to change your colors, so your only adding 1, 2, 3, and 4 to your 200 which won't be noticeable. You might try using a larger number than 4 when you increment color as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top