Search code examples
iosuiviewuiscrollviewbackground-coloruicolor

UIView background color not changing


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

    }

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.