How to use the ScrollContainer component

The ScrollContainer class is a generic container that can position its children using a layout. This type of container offers support for scrolling content that is too large to fit within its visible bounds. Scrolling is supported in a variety of ways — including touch and drag, mouse wheel and scroll bars, or by using a keyboard's arrow keys when the container has focus.

If you don't need scrolling, consider using the LayoutGroup component instead.

Live preview of the ScrollContainer component

The Basics

Create a ScrollContainer component, add it to the display list, and add some children.

var container = new ScrollContainer();
addChild(container);

var child1 = new Label();
child1.text = "Hello";
container.addChild(child1);

var child2 = new Sprite();
child2.graphics.beginFill(0xcccccc);
child2.graphics.drawRect(0.0, 0.0, 100.0, 100.0);
child2.graphics.endFill();
container.addChild(child2);

A mix of OpenFL's core display objects and other Feathers UI components may be added to the container.

Set the container's layout property to automatically position its children.

container.layout = new HorizontalLayout();

The example above uses HorizontalLayout, but a number of different layouts are available in Feathers UI, and it's also possible to create custom layouts.

Styles

A number of styles may be customized on a ScrollContainer component, including an optional background skin the appearance of the container's scroll bars.

Background skin

Optionally give the container a background using the backgroundSkin property. The following example sets it to a RectangleSkin instance.

var skin = new RectangleSkin();
skin.border = SolidColor(1.0, 0x999999);
skin.fill = SolidColor(0xcccccc);
skin.width = 16.0;
skin.height = 16.0;
container.backgroundSkin = skin;

The border and fill properties of the RectangleSkin are used to adjust its appearance. They support a variety of values — from solid colors to gradients to bitmaps.

The container automatically calculates its preferred size based on the initial dimensions of its background skin (accounting for some other factors too, like the size and position of children), so it's important to set a skin's width and height properties to appropriate values to use in this calculation.

See Skinning with common shapes for more details about how to use RectangleSkin with the LineStyle and FillStyle enums that change its border and fill appearance.

The appearance of the container's border or fill may be customized to change when the container is disabled. In the next example, setting the skin's disabledFill method makes it switch to a different fill when the container is disabled.

skin.disabledFill = SolidColor(0xffcccc);

Similarly, use the skin's disabledBorder property to change the border when disabled.

skin.disabledBorder = SolidColor(2.0, 0x999999);

In the examples above, the container uses the same RectangleSkin for all states, and that skin listens for changes to the container's current state. Alternatively, the container's disabledBackgroundSkin method allows the container to display a completely different display object when it is disabled.

var defaultSkin = new RectangleSkin();
// ... set border, fill, width, and height
container.backgroundSkin = defaultSkin;

var disabledSkin = new RectangleSkin();
// ... set border, fill, width, and height
container.disabledBackgroundSkin = disabledSkin;

In the example above, the container will have a separate skins when enabled and disabled.

Scroll bars

The scroll bars in a ScrollContainer component are of type HScrollBar and VScrollBar. Their appearance may be customized globally in a theme, or they may be customized outside of a theme on an specific, individual scroll container.

See How to use the HScrollBar and VScrollBar components for complete details about which styles are available for the scroll bars.

Style scroll bars globally

Use the HScrollBar and VScrollBar classes in a theme to provide a function that globally styles all scroll bars in your project.

styleProvider.setStyleFunction(HScrollBar, null, setHScrollBarStyles);
styleProvider.setStyleFunction(VScrollBar, null, setVScrollBarStyles);

The functions should use the following signatures.

function setHScrollBarStyles(scrollBar:HScrollBar):Void {
    // ... set styles here
}

function setVScrollBarStyles(scrollBar:VScrollBar):Void {
    // ... set styles here
}

Style scroll bars in a specific ScrollContainer

The scrollBarXFactory and scrollBarYFactory properties may be used to customize the creation of an individual container's scroll bars.

container.scrollBarXFactory = () -> {
    var scrollBar = new HScrollBar();
    // ... set styles here
    return scrollBar;
};

container.scrollBarYFactory = () -> {
    var scrollBar = new VScrollBar();
    // ... set styles here
    return scrollBar;
};