How to Resize the Flex Stage and Use the Browser Scrollbar

I have a Flex application that will shrink and grow vertically, but I do NOT want to ever, ever, ever see the Flex scroll bar. Ever! It has its place, but not for the entire app. That’s what the browser has a scroll bar for. And people are intimately familiar with that scroll bar, so I would much rather use that.

My Flex application makes use of the mx:ViewStack and each view will be the same width, but vary in height and even change interactively. At a minimum I want Flash to span the available browser window top to bottom. At a maximum I want the user to scroll with the browser scroll bar.

How I Did It
I disable the Flex scroll bars by modifying the policy value. Depending on how nested your application is (Canvas > VBox > Canvas > HBox > etc) you may have to do this at many levels. If there is a way to disable this by default, please have mercy on a brother and tell me now.

  1.  
  2. <mx:Application
  3. xmlns:mx="http://www.adobe.com/2006/mxml"
  4. layout="absolute"
  5. width="680"
  6. horizontalScrollPolicy="off"
  7. verticalScrollPolicy="off">
  8.  

Next I set the ViewStack to resizeToContent.

  1.  
  2. <mx:ViewStack id="mainStack" resizeToContent="true"
  3. <views:WelcomeView id="welcomeView" />
  4. <views:OrderFormView id="orderFormView" />
  5. </mx:ViewStack>
  6.  

Now I create a listener to handle any resize of the views. In my case this will always come up through the OrderFormView which is itself an mx:ViewStack.

  1.  
  2. import mx.events.ResizeEvent;
  3.  
  4. private function setupResize() : void
  5. {
  6. orderFormView.addEventListener(ResizeEvent.RESIZE, formResize);
  7. }
  8.  

And then on formResize I use the ExternalInterface to issue the appropriate javascript to resize my DIV container to the height of my ViewStack plus a buffer of 30 pixels cuz I want a little play at the bottom.

  1.  
  2. private function formResize(e : ResizeEvent) : void
  3. {
  4. ExternalInterface.call("setFlashHeight", mainStack.height + 30);
  5. }
  6.  

ExternalInterface.objectID is the ID of the DIV made available by Flex. Then in the page I have a javascript function to perform the actual resize.

  1.  
  2. function setFlashHeight(newHeight)
  3. {
  4. var viewportwidth;
  5. var viewportheight;
  6.  
  7. // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
  8.  
  9. if (typeof window.innerWidth != 'undefined')
  10. {
  11. viewportwidth = window.innerWidth,
  12. viewportheight = window.innerHeight
  13. }
  14.  
  15. // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
  16. else if (typeof document.documentElement != 'undefined'
  17. && typeof document.documentElement.clientWidth !=
  18. 'undefined' && document.documentElement.clientWidth != 0)
  19. {
  20. viewportwidth = document.documentElement.clientWidth,
  21. viewportheight = document.documentElement.clientHeight
  22. }
  23.  
  24. // older versions of IE
  25. else
  26. {
  27. viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
  28. viewportheight = document.getElementsByTagName('body')[0].clientHeight
  29. }
  30.  
  31. document.getElementById('consumer').style.height = (viewportheight > newHeight) ? viewportheight + 'px' : newHeight + 'px';
  32. }
  33.  


Something else you’ll likely want to do is scroll back to the top of the page after the resize.

  1.  
  2. ExternalInterface.call(
  3. "function() { scroll(0,0); }"
  4. );
  5.  

That’s it. The next thing to do is implement something like SWFFIT or write a little function that appropriately resizes the DIV upon a browser resize event (if the user resizes or removes a status bar or finder bar, etc.).


Categories: FlashFlexPermalink
blog comments powered by Disqus