Main Content

uistack

Reorder visual stacking of UI components

Description

uistack(comp) shifts the specified UI component up one level within the visual stacking order of UI components. If comp is a vector of UI components, uistack shifts each component in the vector up one level.

example

uistack(comp,moveto) moves the UI component to the specified position in the stack. For example, uistack(c,"top") moves component c to the top of its stack.

example

uistack(comp,moveto,step) specifies the number of steps to move the UI component up or down. For example, uistack(c,"up",2) moves c up two levels in its stack.

example

Examples

collapse all

Create a UI figure with a button and a gauge. Because the gauge is created second, it appears in the front and covers the button.

fig = uifigure;
btn = uibutton(fig,"Text","Go","Position",[148 148 25 25]);
g = uigauge(fig);

Figure contains objects of type uibutton, uigauge.

Bring the button to the front by moving it to the top of the stack.

uistack(btn,"top");

Figure contains objects of type uigauge, uibutton.

Create a UI figure that contains five overlapping panels with different titles and background colors.

fig = uifigure;
a = uipanel(fig,"Title","A","BackgroundColor","white","Position",[200 200 170 150]);
b = uipanel(fig,"Title","B","BackgroundColor","cyan","Position",[100 170 170 150]);
c = uipanel(fig,"Title","C","BackgroundColor","green","Position",[50 90 170 150]);
d = uipanel(fig,"Title","D","BackgroundColor","yellow","Position",[140 140 170 150]);
e = uipanel(fig,"Title","E","BackgroundColor","magenta","Position",[170 110 170 150]);

Figure contains objects of type uipanel.

List the children in the UI figure.

fig.Children
ans = 
  5x1 Panel array:

  Panel    (E)
  Panel    (D)
  Panel    (C)
  Panel    (B)
  Panel    (A)

Reorder the panels by shifting panels a and c up one level in the stack relative to their current positions.

comp = [a c];
uistack(comp)

Figure contains objects of type uipanel.

List the children again. Changing the stacking order of the panels also changes the order of the children in the UI figure.

fig.Children
ans = 
  5x1 Panel array:

  Panel    (E)
  Panel    (C)
  Panel    (D)
  Panel    (A)
  Panel    (B)

Create a UI figure that contains a tree with two top-level nodes, each with two child nodes.

fig = uifigure;
t = uitree(fig,"Position",[20 20 150 150]);
category1 = uitreenode(t,"Text","Vegetables");
category2 = uitreenode(t,"Text","Fruits");
n1 = uitreenode(category1,"Text","Cucumber");
n2 = uitreenode(category1,"Text","Carrot");
n3 = uitreenode(category2,"Text","Apple");
n4 = uitreenode(category2,"Text","Banana");

Figure contains an object of type uitree.

Expand all of the nodes in the tree.

expand(t)

Figure contains an object of type uitree.

Move the Fruits node to the top of the stack. The node reorders relative to all other nodes that share a parent. This command moves the Fruits node and all of its child nodes above the Vegetables node.

uistack(category2,"top")

Figure contains an object of type uitree.

Rearrange the child nodes of the Fruits node. Move the Apple node one level down, below the Banana node.

uistack(n3,"down",1)

Figure contains an object of type uitree.

Create a UI figure that contains a tab group with five tabs.

fig = uifigure;
tg = uitabgroup(fig,"Position",[20 20 450 300]);
t1 = uitab(tg,"Title","Survey Questions");
t2 = uitab(tg,"Title","Demographic");
t3 = uitab(tg,"Title","Participants");
t4 = uitab(tg,"Title","Data");
t5 = uitab(tg,"Title","Plot");

Figure contains an object of type uitabgroup.

Move the Data tab to the bottom of the stack. The tab appears all the way on the right.

uistack(t4,"bottom")

Figure contains an object of type uitabgroup.

Move the Survey Questions tab two levels to the right.

uistack(t1,"down",2)

Figure contains an object of type uitabgroup.

Input Arguments

collapse all

UI component to reorder, specified as a single object or vector of objects, such as Figure, Panel, Tab, Button, or TreeNode objects. Use this argument to specify the UI components you want to reorder relative to other objects that share the same parent.

If comp is specified as a vector of UI components:

  • Each component in the vector must share a parent.

  • The vector must be a strict subset of the children of the parent container. For example, if a figure has six child UI components, the vector comp can have no more than five elements.

Location to move a UI component, specified as one of the values in this table.

ValueDescription
"up"Move the UI component up step levels (one level by default).
"down"Move the UI component down step levels (one level by default).
"top"Move the UI component to the top of its stack.
"bottom"Move the UI component to the bottom of its stack.

Number of levels to shift a UI component up or down in a stack, specified as a positive integer. Use this argument only when moveto is specified as "up" or "down".

If you specify a step number that is greater than the number of stack levels available to move, then the UI component moves to the top or bottom of the stack. For example, if you have five stack levels and you specify a component to move down six steps, that component moves to the bottom of the stack.

Version History

Introduced before R2006a

expand all