Garry's Mod Wiki

Panel:SizeToChildren

  Panel:SizeToChildren( boolean sizeW = false, boolean sizeH = false )

Description

Resizes the panel to fit the bounds of its children.

Your panel must have its layout updated (Panel:InvalidateLayout) for this function to work properly.
The sizeW and sizeH parameters are false by default. Therefore, calling this function with no arguments will result in a no-op.

Arguments

1 boolean sizeW = false
Resize with width of the panel.
2 boolean sizeH = false
Resize the height of the panel.

Example

local Frame = vgui.Create( "DFrame" ) Frame:SetSize( 300, 400 ) Frame:Center() Frame:MakePopup() Frame:SetSizable( true ) -- with :InvalidateLayout(true) local backgroundPanel = vgui.Create( "DPanel", Frame ) backgroundPanel:Dock( TOP ) backgroundPanel:DockPadding( 4, 4, 4, 4 ) backgroundPanel:DockMargin( 0, 0, 0, 4 ) local button1 = vgui.Create( "DButton", backgroundPanel ) button1:Dock( TOP ) button1:DockMargin( 0, 0, 0, 4 ) button1:SetTall( 60 ) button1:SetText( "c1" ) local button2 = vgui.Create( "DButton", backgroundPanel ) button2:Dock( TOP ) button2:SetTall( 60 ) button2:SetText( "c2" ) backgroundPanel:InvalidateLayout( true ) backgroundPanel:SizeToChildren( false, true ) -- now w/o :InvalidateLayout local backgroundPanel = vgui.Create( "DPanel", Frame ) backgroundPanel:Dock( TOP ) backgroundPanel:DockPadding( 4, 4, 4, 4 ) local button1 = vgui.Create( "DButton", backgroundPanel ) button1:Dock( TOP ) button1:DockMargin( 0, 0, 0, 4 ) button1:SetTall( 60 ) button1:SetText( "c1" ) local button2 = vgui.Create( "DButton", backgroundPanel ) button2:Dock( TOP ) button2:SetTall( 60 ) button2:SetText( "c2" ) backgroundPanel:SizeToChildren( false, true )
Output: