Scaling ScrollingFrame CellPadding

I created a script to scale the CanvasSize of a ScrollingFrame based on it’s contents. All was a success except for adjusting the CellPadding property of a UIGridLayout to match nicely with the CanvasSize.

I’ve tried many solutions such as multiplying the padding by the ScrollingFrame’s AbsoluteSize property however it left a massive gap between each cell. If anyone has any experience with doing this, please let me know or give me ideas.

1 Like

So 2 questions:

  1. What does your current code look like, if you have any?

  2. What do you mean by “match nicely”? Do you mean like filling the rest of the scrolling frame with empty space if the contents don’t fill the frame’s normal size? Do you mean giving the contents less padding with more items?

Code that scales the ScrollingFrame:

local OffsetY = Shop.List.UIGridLayout.AbsoluteContentSize.Y
Shop.List.CanvasSize = UDim2.new(0, 0, 0, OffsetY + 20)

By match nicely, I mean have the gap between each cell be the same no matter what the CanvasSize is set to. I need help figuring out how to make that happen because at the moment, the larger the CanvaseSize, the larger the gap.

The simplest way to make the padding constant no matter the CanvasSize is using the second argument of Padding, which is pixel-by-pixel offset.
e.g,

UIGridLayout.Padding = UDim.new(0,10)
1 Like

adding on to this, you could scale the offset padding via script


local Grid = script.Parent
local Frame = Grid.Parent.Frame

local Percentage = .2


function ScalePadding() -- scales padding based on a percentage of a frame's size (you could scale with CanvasSize too)
	
	local AB = Frame.AbsoluteSize -- AbsoluteSize is the part's size in pixels(offset)
	local Ax = AB.X * Percentage  -- get a percentage of the offset of the frame (only using X or Y and not both because the padding wouldn't match if we used both and the Frame sizes arent 1:1 size ratio)

	Grid.CellPadding = UDim2.new(0, Ax, 0, Ax)
	
end

Frame:GetPropertyChangedSignal("AbsoluteSize"):Connect(ScalePadding)

this is my take on it, you could probably just take the general idea and apply it with CanvasSize

same scaled offset padding even with non 1:1 size ratio elements