iosobjective-cuiviewalphaviewwithtag

Make UIViews with tags transparent and back again


I am making a bar chart in Objective-C by first making an empty chart in viewDidLoad and then redrawing the heights of the bars when data is downloaded from a web service. My method for grabbing the bars is to give them a tag and then grab them once I have the data using their tag.

During the brief interval of waiting for the data, the bars are not the right size. Accordingly, I am trying to figure out a way to make them invisible while we gather the data. However, when I try setting alpha to 0 or setting the color to clearColor, they disappear but I can't make them reappear when I redraw them.

Here is my code to create the bars:

for (i = 0; i < _numBars; i++) {
    CGRect localBar = CGRectMake(gutterWidth +(increment*i), 50, barWidth, barHeight);
    UIView *localBarView = [[UIView alloc] initWithFrame:localBar];
        
    localBarView.tag = 10+i*100;
    localBarView.backgroundColor = [UIColor blueColor];
  
    //HERE IS WHERE I MAKE IT TRANSPARENT
    localBarView.backgroundColor = [UIColor clearColor];
    localBarView.alpha =0.0;
    [self.graphView addSubview:localBarView];
}
 

Here is where I reset their heights via their frame.

int i;
for (i = 0; i < _numBars; i++) {//open 2
    int tag = 10+(100*i);
    if ([self.view viewWithTag:tag]) {
        grabBar =(UIView*)[self.view viewWithTag:tag];
        val = [barValues[i] intValue];
        //HERE IS WHERE I TRY TO SET THE COLOR TO BLUE BUT THEY REMAIN INVISIBLE
        grabBar.backgroundColor = [UIColor blueColor];
        grabBar.alpha = 1.0;/
       
        //set height of bar based on value from webservice
        barFrame.size.height=[self calculateHeightFromVal]; 
        grabBar.frame=barFrame;
        //This code does work to redraw the heights
    }

How can I make the bars visible again when I redraw them?


Solution

  • Your two sets of code are using two completely different parent views and viewWithTag: is not recursive, it only looks at the view itself and its immediate subviews.

    Since your first set of code is adding the hidden views to self.graphView, your second set of code also needs to check self.graphView.

    In the second set of code, change:

    if ([self.view viewWithTag:tag]) {
        grabBar =(UIView*)[self.view viewWithTag:tag];
    

    to:

    if ([self.graphView viewWithTag:tag]) {
        grabBar =(UIView*)[self.graphView viewWithTag:tag];