views:

985

answers:

5

One of my components looks like this:

<mx:Canvas id="grid" width="100%" height="100%"></mx:Canvas>

On creationComplete, I load some spirte that I want to scale and position based on the dimensions of the canvas to create a custom grid layout, but when I access the dimensions of 'grid' I get 0 and 0. Is there any way to get the dimensions without assigning absolute values?

A: 

I can't check this at the moment but can you access the measuredHeight and measuredWidth?

Ryan Guill
Nope, they give me 0,0 too.
A: 

My understanding is that unless the Canvas contains one or more DisplayObject children, it will always report its width and height properties as 0, regardless of the percentage sizings you may have applied to it.

You could always add an empty dummy DisplayObject to the Canvas, but that wouldn't be very elegant. Depending on how you've planned to implement your custom grid, it's possible you'll have to rethink the design...

Stiggler
I tried this hack yesterday: drop in some random component, measure the canvas, remove all the children. It still says the height and width are 0. I must say, this is all very unintuitive. I was hoping it would work like the HTML DOM.
I tried this prior to writing and it worked for me. The child component must measure something itself in order for the parent canvas to report w/h. Did the child component have a chance to get fully laid out prior to you checking the measurements?
Stiggler
A: 

Is it possibly created but not yet displayed (e.g. visible)? Since final size and shape is derived, it doesn't happen until the thing actually needs to be drawn. Width and height are documented to be the values actually in use - there are even events for when they change.

Worst case, try trapping it out in the canvas Resize event.

le dorfier
It doesn't seem to matter when I access the dimensions. If I draw a border around it, I get the width and height of the borders back (2x2)
A: 

This is driving me crazy too. I am adding a label and a datagrid to a VBox container. Both of them have height = 0, width = 0. And I Want to get their dimensions so that I can resize the parent container.

A: 

have you tried binding the width and height of the Canvas to its parent? If it is 100%, than it will have the same size as its parent and you could either bind or size your Sprite (UIComponent) based on the parent container of the Canvas.

Joel Hooks
Unfortunately, its parent reports the same dimensions.
If the parent's size is 0,0 then sizing to 100% will yield 0,0. Is it possible for you post the whole MXML hierarchy you're dealing with?
Niko Nyman