Application top level page definitions, and reusable component definitions, should use one QML layout definition for the layout structure. This single definition should include the layout design for separate Device Orientations and container Aspect Ratios. The reason for this is that performance during an orientation switch is critical, and it is therefore a good idea to ensure that all of the components needed by both orientations are loaded when the containing Page is loaded.
On the contrary, you should perform thorough tests if you choose to use a Loader to load additional QML that is needed in separate orientations, as this will affect the performance of the orientation change.
In order to enable layout animations between the orientations, the anchor definitions must reside within the same containing component. Therefore the structure of an page or a component should consist of a common set of child components, and a common set of anchor definitions (defined in a StateGroup):
StateGroup { states: [ State { when: (true) AnchorChanges { target: infoArea; anchors.left: helper.left; anchors.top: helper.top } AnchorChanges { target: controlArea; anchors.right: helper.right } } ] }
and a collection of states (defined in another StateGroup) representing the different aspect ratios supported by the component.
StateGroup { states: [ State { when: (container.width / container.height) > 3.00 AnchorChanges { target: infoArea; anchors.right: undefined; anchors.bottom: helper.bottom } AnchorChanges { target: frequencyArea; anchors.left: infoArea.right; anchors.right: helper.right; anchors.top: undefined; anchors.bottom: parent.bottom } AnchorChanges { target: controlArea; anchors.left: infoArea.right; anchors.top: helper.top; anchors.bottom: frequencyArea.top } }, State { when: (container.width / container.height) > 1.50 AnchorChanges { target: infoArea; anchors.right: undefined; anchors.bottom: frequencyArea.top } AnchorChanges { target: frequencyArea; anchors.left: helper.left; anchors.right: infoArea.right; anchors.top: undefined; anchors.bottom: parent.bottom } AnchorChanges { target: controlArea; anchors.left: infoArea.right; anchors.top: helper.top; anchors.bottom: helper.bottom } }, State { when: (container.width / container.height) > 1.00 AnchorChanges { target: infoArea; anchors.right: undefined; anchors.bottom: frequencyArea.top } AnchorChanges { target: frequencyArea; anchors.left: helper.left; anchors.right: helper.right; anchors.top: undefined; anchors.bottom: parent.bottom } AnchorChanges { target: controlArea; anchors.left: infoArea.right; anchors.top: helper.top; anchors.bottom: frequencyArea.top } }, State { when: (true) AnchorChanges { target: infoArea; anchors.right: helper.right; anchors.bottom: undefined } AnchorChanges { target: frequencyArea; anchors.left: helper.left; anchors.right: helper.right; anchors.top: infoArea.bottom; anchors.bottom: undefined } AnchorChanges { target: controlArea; anchors.left: helper.left; anchors.top: frequencyArea.bottom; anchors.bottom: helper.bottom } } ] }
One advantage of defining the layouts within states, rather than within the component definitions, is that it helps with maintainability, particularly when layout animations are also defined in the corresponding state transitions. (However note that orientation change animations are not possible on Symbian due to compatibility support for S60 applications).
If a component, contained within a Page element, needs to be hosted in numerous different form factor definitions, then the layout states of the view should depend on the aspect ratio of the page (its immediate container). Similarly, different instances of a component might be situated within numerous different containers in a UI, and so its layout states should be determined by the aspect ratio of its parent. The conclusion is that layout states should in general follow the aspect ratio of the direct container (not the current Device Orientation). The only exception to this is of course the top level Window definition, which is handled by the framework, or some parameters that happen to depend specifically on the Device Orientation.
Within each layout State, you should define the relationships between items using native QML layout definitions. See below for more information. During transitions between the states (triggered by the top level orientation change), in the case of anchor layouts, AnchorAnimation elements can be used to control the transitions. In some cases, you can also use a NumberAnimation on e.g. the width of an item. Remember to avoid complex javascript calculations during each frame of animation. Using simple anchor definitions and anchor animations can help with this in the majority of cases.
There are a few additional cases to consider: