Pregunta

Estoy declarando varias vistas de uives que se agregan a mi vista UIScroll en un bucle for, me gustaría saber cómo cambiar el color de cada fondo UIView cuando progresa el bucle. Actualmente, cada UIView simplemente permanece del mismo color gris.

Así es como se ve mi código:

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

    }
¿Fue útil?

Solución

El problema está en esta línea:

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

Cada vez que el rojo, el verde y el azul tienen el mismo valor, el color es gris.

Agregue un valor constante diferente al de 200 a, al menos, un componente rojo y verá diferentes colores.

Otros consejos

El problema es que está agregando "i" en lugar de "color" cuando intenta cambiar sus colores, por lo que su único agregado 1, 2, 3 y 4 a sus 200, que no se notarán. Puede intentar usar un número mayor que 4 cuando incrementa el color también.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top