Resize (ScrollingFrame)

Hello, I hope you are feeling well! I want to make that once I add a GuiObject to a ScrollingFrame, this one resizes the CanvasSize without affecting the size of the GuiObject. I want all to use Scale and not Offset. I am having a lot of issues trying to make such thing, I have tried the Layout plugin and it didn’t worked. I have lost like a lot of brain cells in these two days trying to figure it out how to do such thing without using any other StyleLayout. I have this thing so far but it doesn’t work properly either.

local LayoutSizes = {}
local function Adjust(ScrollingFrame)
	local UIGridLayout = ScrollingFrame:FindFirstChildOfClass("UIGridLayout")
	if UIGridLayout == nil then return end
	
	if LayoutSizes[UIGridLayout] == nil then LayoutSizes[UIGridLayout] = UIGridLayout.CellSize end
	local function GetAbsoluteSize()
		local XScale, YScale = LayoutSizes[UIGridLayout].X.Scale, LayoutSizes[UIGridLayout].Y.Scale
		return UDim2.new(0, XScale*ScrollingFrame.AbsoluteSize.X, 0, YScale*workspace.CurrentCamera.ViewportSize.Y)
	end
	
	local Size, Count = GetAbsoluteSize(), #ScrollingFrame:GetChildren()
	UIGridLayout.CellSize = Size
	ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, Count*(Size.Y.Offset))
end

--- The Piece of Code below is for testing purposes --- :)
wait(5)
local ScrollingFrame = script.Parent.ScreenGui.ScrollingFrame
Adjust(ScrollingFrame)
ScrollingFrame.ChildAdded:Connect(function()
	Adjust(ScrollingFrame)
end)

ScrollingFrame.ChildRemoved:Connect(function()
	Adjust(ScrollingFrame)
end)

Have in mind I just want to have a ScrollingFrame which has a UIGridLayout in it and every time I add a GuiObject, the ScrollingFrame should resize the canvas size without affecting anything else. I want also to be able to call a function that adjusts it. In other words, if I clone something and insert it in the ScrollingFrame at run time, I will also call the function so it resizes the ScrollingFrame.

If you know please help me, cuz I am feeling like I wasted two days trying to do such task…

While I’m not very sure how it’d be done via scale, the following post provides an explanation of how this could be accomplished by adjusting the Offset of the ScrollingFrame’s CanvasSize upon detecting changes to the AbsoluteContentSize property of the UIGridLayout.

Because of how the function is setup in the post, this means that you won’t need to manually call a function when you add something, as adding any new GuiObjects into the ScrollingFrame will call it automatically.


From my experiences utilizing this for a custom playerlist, the GuiObjects remain unaffected and the ScrollingFrame automatically adjusts to provide enough room for the new cells to be included inside of the frame.

2 Likes