简体   繁体   English

UIView 背景颜色没有改变

[英]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.我在 for 循环中声明了几个添加到我的 UIScroll 视图中的 UIView,我想知道如何在循环进行时更改每个 UIView 背景的颜色。 Currently each UIVIew just stays the same color grey.目前每个 UIVIew 只是保持相同的灰色。

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

    }

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.至少将与 200 不同的常量值添加到红色组件,您将看到不同的颜色。

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.问题在于,当您尝试更改颜色时,您添加了“i”而不是“颜色”,因此您只会在 200 中添加 1、2、3 和 4,这不会引起注意。 You might try using a larger number than 4 when you increment color as well.当您增加颜色时,您也可以尝试使用大于 4 的数字。

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

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