PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

AbsoluteContentSize

UIGridStyleLayout

Vector2

The AbsoluteContentSize property of a UIGridStyleLayout shows how much space the elements of the grid are taking up, including any padding created by the grid. This property is particularly useful to size containers of grids such as Frames and Frame to make sure they aren’t any larger than the grid itself.

It is important to note that this property does not get updated immediately. When a grid’s size is changed the AbsoluteContentSize will be updated on the next render step. When using AbsoluteContentSize to determine the size of a container element, it is recommended to either listen for the property to change with the Frame, or by waiting for the RunService/RenderStepped with Frame.


Code Samples


AbsoluteContentSize

In this example a UIGridLayout is created inside of a ScrollingFrame. To make sure the ScrollingFrame/CanvasSize is exactly the size it needs to be to contain the grid, a function is connected to the grids Instance/GetPropertyChangedSignal to listen for when the AbsoluteContentSize of the grid changes, and sets the canvas size accordingly.

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

local screen = Instance.new("ScreenGui")
screen.Parent = playerGui

local scrollingFrame = Instance.new("ScrollingFrame")
scrollingFrame.Parent = screen
scrollingFrame.AnchorPoint = Vector2.new(0.5, 0.5)
scrollingFrame.Position = UDim2.new(0.5, 0, 0.5, 0)
scrollingFrame.Size = UDim2.new(0, 412, 0, 300)

local grid = Instance.new("UIGridLayout")
grid.CellPadding = UDim2.new(0, 0, 0, 0)
grid.CellSize = UDim2.new(0, 100, 0, 100)
grid.Parent = scrollingFrame

local function onContentSizeChanged()
	local absoluteSize = grid.AbsoluteContentSize
	scrollingFrame.CanvasSize = UDim2.new(0, absoluteSize.X, 0, absoluteSize.Y)
end

grid:GetPropertyChangedSignal("AbsoluteContentSize"):Connect(onContentSizeChanged)

for x = 1, 10 do
	for y = 1, 4 do
		local button = Instance.new("TextButton")
		button.Text = x .. ", " .. y
		button.Parent = scrollingFrame
	end
end