tags:

views:

213

answers:

1

I have a function I wrote that gets the height of the application without scrolling. Meaning the height it would need to be to not have to scroll.

This is the function:

private function getContentHeight():Number
            {
                var height:Number = 0;
                for(var i:uint = 0; i<this.numChildren; i++)
                {
                 var d:DisplayObject = this.getChildAt(i) as DisplayObject;
                 //trace(d.name+":"+d.height);
                 height += d.height;
                }
                height += Number(this.getStyle('verticalGap'))*this.numChildren;
                height += Number(this.getStyle('paddingTop'));
                height += Number(this.getStyle('paddingBottom'));
                return height;
            }

This function works well but the problem is knowing when to call it. I planned on calling it after I add/remove any children from it. However thats still too soon because I don't get the correct height yet if I run it then. The fix I currently have is using a Timer to call the function about 300 milliseconds after a child is added/removed which seems to work well but I think this is a very risky fix. So I was wondering a better solution. I am thinking there has to be some sort of event I can listen for that will tell me when its ready for me to run this function, I just don't know what event that would be?

Thanks!

+1  A: 

First of all, take a look at measuredWidth and measuredHeight properties of UIComponent (so all the controls and containers have these properties), flex already measures required width/height of the containers.

Secondly, the right place to look at this properties are creationComplete and updateComplete events. In your case, if you add some children programmaticaly, you should handle updateComplete event after you've added you children.

Hrundik