UI scrolling expansion when an item is added to UIListLayout

I want to make it so the scrollbar extends to the end of the last button. I am wondering if anyone else can help me, with examples of what their solutions are, to my request. Thanks a lot!

2 Likes

You can easily do that with a simple equation:

canvasSize = totalObjectSize + ((numerOfObjects - 1) * margin) + marginStart + marginEnd

It seems long at first, but it’s easy to implement. marginStart and marginEnd only work when the vertical alignment is set to center. If it’s set to top or bottom, then it’d only allow margin on one side (the beginning or the end). Let’s just assume that it is indeed center.

Here’s an issue though: UIListLayout does not restrict the size the objects, they can be different from each other, so we need to add them up:

local objects = scrollingFrame:GetChildren()
local totalSize = 0
local marginStart, marginEnd = 10, 10
local margin = scrollingFrame.UIListLayout.Padding.Offset

for i, v in pairs(objects) do

    if not v:IsA("UIListLayout") then

        totalSize = totalSize + v.Size.Y.Offset

    end

end

local canvasSize = totalSize + ((#objects - 2) * margin) + marginStart + marginEnd

--why #objects - 2 and not -1? Well, we need to do that to exclude the list layout

scrollingFrame.CanvasSize = UDim2.new(0, 0, 0, canvasSize)

I know I did this with offset, but a similar thing goes for scale, too, just in decimals. Hope that helps!

2 Likes

Try and expand Y axis on the Canvas size in the properties panel. :+1:

1 Like

Thanks a lot! I will use your method for my script.

1 Like