Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting background in Objective C #230

Closed
tbrennanw0rk opened this issue Jul 20, 2015 · 2 comments
Closed

Setting background in Objective C #230

tbrennanw0rk opened this issue Jul 20, 2015 · 2 comments

Comments

@tbrennanw0rk
Copy link

Hi,

I have been struggling for a few days on this aspect, creating a background image using CALayer. The aim is to have a coloured background behind a combined chart view that I am working on which populates the data series.

  1. Create the view that matches the chart's layout, idea being, is that we can add the subview to the chart when drawing is done.

    UIView * uiVw = [[UIView alloc] initWithFrame:self.combinedChartView.bounds];
  2. Create a layer, to do the drawing operations on it.

    CALayer *layer = [CALayer layer];
    layer.zPosition = -1;
    layer.bounds = self.comboChartView.bounds;
  3. The drawing background using layers x times over within the loop is performed

    for (int i = 0; i < portionsCount; i++){
         UIColor *selectedColor = ((i % 2) == 0) ? shadeOfGray : shadeOfBlue;
         CALayer *subLayer = [CALayer layer];
         subLayer.backgroundColor = selectedColor.CGColor;
         /* the positioning arrangement of layer is calculated */
         subLayer.opacity = 0.5f;
         subLayer.zPosition = -1 - i; 
         [layer addSublayer:sublayer];
     }
  4. The layer gets added into the view

    [uiVw.layer addSublayer:layer];
  5. Finally, the view gets added to the combo chart view

    [self.comboChartView addSubview:uiVw];

FWIW, the background looks like a chunky bar chart, (its more of a alternating colors to identify the range where the pertinent data fell on a particular series).

What was expected to happen was the background, get rendered beneath the combo chart. Instead I get the background obscuring the chart itself, despite having the zPosition to be in negative.

Any ideas?

@danielgindi
Copy link
Collaborator

The zPosition means something entirely different than z-index. It's actually the rotation on the z axis.
You have to actually put the layer behind the chart's layer, which means having the correct order in the layers array.
Then set backgroundColor on the chart's view to transparent and opaque to NO.

@tbrennanw0rk
Copy link
Author

Did not realize about zPosition's actual meaning.

Thanks for the prompt response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants