Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

ScrollingFrame with a CanvasSize depending on descendant count?

I have a ScrollingFrame and there can be infinite descendants, and I want that frame to have a CanvasSize large enough to scroll through each and every descendant.

If you know the answer, then please reply.

Thanks!

2 answers

Yes, I was pondering this just yesterday. I think the AbsoluteSize and TextBounds properties should help you.

1 comment

0

ScrollingFrames are not text containers, I just need code to increase the size of the CanvasSzie depending on descendant count.

assuming you're using a UIListLayout or a UIGridLayout to lay out each ui element in the ScrollingFrame, you can use the AbsoluteContentSize property to automatically resize it

UIListLayout:GetPropertyChangedSignal("AbsoluteContentSize"):Connect(function()
	ScrollingFrame.CanvasSize = UDim2.new(0, UIListLayout.AbsoluteContentSize.X, 0, UIListLayout.AbsoluteContentSize.Y)
end)
ScrollingFrame.CanvasSize = UDim2.new(0, UIListLayout.AbsoluteContentSize.X, 0, UIListLayout.AbsoluteContentSize.Y) -- update it if there are already ui elements in the frame

otherwise you'll have to manually iterate over descendants, and find the position of the outermost element

local maxX = -math.huge -- in case you have elements with a negative position for...some reason
local maxY = -math.huge
for _, child in pairs(ScrollingFrame:GetChildren()) do -- if the elements have their own children which go outside their bounds then you'll have to change this to GetDescendants
	maxX = math.max(maxX, child.AbsolutePosition.X + child.AbsoluteSize.X)
	maxY = math.max(maxY, child.AbsolutePosition.Y + child.AbsoluteSize.Y)
end
ScrollingFrame.CanvasSize = UDim2.new(0, maxX - ScrollingFrame.AbsolutePosition.X, 0, maxY - ScrollingFrame.AbsolutePosition.Y)

This discussion is preserved as it was. New answers, comments, and votes are closed.