简体   繁体   中英

Finding Color of a UIView Object and change the Color

在此处输入图片说明 Hello Guys actually i have a lot of UIView custom class objects as a subview of a UIViewController class view which is inside a UIScrollView. And i want to check colors of UIView custom class objects. code i use is given below:-

    - (void) RectColorCheck:(id)sender
{
    NSArray *subViews = [[NSArray alloc] init];
    subViews = [self.scrollView subviews];
    NSLog(@"array----%@",subViews);

        for (viewCG in subViews) 
        {
            if ([viewCG.backgroundColor isEqual: [UIColor darkGrayColor]])
            {
                [viewCG setBackgroundColor:[UIColor orangeColor]];
            }
        }
}

but it not works the subview array of viewCG is null. And the below code which adds the custom class(UIView) objects in viewCG subviews:-

for (int i=0; i<[mapDataArr count]; i++)
    {
        X = [[[mapDataArr objectAtIndex:i] valueForKey:@"X"] intValue];
        Y = [[[mapDataArr objectAtIndex:i] valueForKey:@"Y"] intValue];
        W = [[[mapDataArr objectAtIndex:i] valueForKey:@"W"] intValue];
        H = [[[mapDataArr objectAtIndex:i] valueForKey:@"H"] intValue];

        circleVwObj = [[CircleView alloc] init];
        circleVwObj.frame = CGRectMake(X,Y, W, H);
        circleVwObj.tag = i;

        circleVwObj.lbl.frame = CGRectMake(2,2, circleVwObj.frame.size.width, circleVwObj.frame.size.height/2);
        circleVwObj.lbl.text = [[mapDataArr objectAtIndex:i] valueForKey:@"standId"];
        NSLog(@"lbl text---%@", circleVwObj.lbl.text);
        circleVwObj.lbl.font = [UIFont boldSystemFontOfSize:11];
        circleVwObj.lbl.backgroundColor = [UIColor clearColor];
        circleVwObj.lbl.textColor = [UIColor whiteColor];
        circleVwObj.lbl.textAlignment = UITextAlignmentCenter;
        circleVwObj.lbl.minimumFontSize = 11;
        circleVwObj.lbl.adjustsFontSizeToFitWidth = YES;
        circleVwObj.lbl.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

        [self.viewCG addSubview:circleVwObj];
    }

If the subviews array is nil , it is very likely that self.viewCG is nil as well and hasn't been initialized.

Also, you should use isEqual: to compare colors. The == operator just compares pointer identity (which might actually give you the expected result in this particular case, but it's likely to break).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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