Autosizing scroll frame based on scale

I have tried to create an auto-sizing scroll frame based on scale, but currently, I have only found solutions based on offset. Anyone have any ideas on how I could accomplish this task? A formulae or something along those lines could help!

1 Like

I am a bit confused what you mean?

Is that what you mean;

-- A neat way to store all your buttons
local Buttons = {}

-- Assuming this is where the scrolling frame is
local Scroll = script.Parent:WaitForChild("ScrollingFrame")

-- Create a new button
local function NewButton()

    -- Parent the button
    local Button = Instance.new("TextButton",Scroll)

    -- Make sure you size it before you position it
    Button.Size = UDim2.new(1,0,0,25)

    -- Position the button based off the amount created, times it's Y offset size
    Button.Position = UDim2.new(0,0,0,#Buttons*Button.Size.Y.Offset)

    -- Return the button
    return Button
end

-- Create one every 1 second
while wait(1) do

    -- Created a new button which is already sized, positioned, and parented.
    local Button = NewButton()

    -- If the button's Position Y Offset plus it's Size Y Offset is greater than the ScrollingFrame's AbsolutePosition Y, then ...

    if Button.Position.Y.Offset+Button.Size.Y.Offset > Scroll.AbsoluteSize.Y then

        -- Set the canvas size to the product of buttons and it's individual size Y offset, plus it's original size (since the first button created is at position 0,0,0,0)
        Scroll.CanvasSize = UDim2.new(0,0,0,#Buttons*Button.Size.Y.Offset+Button.Size.Y.Offset)

    -- If it doesn't exeed the scroll frame, then...
    else

        -- Set it's scrolling size back to 0.
        Scroll.CanvasSize = UDim2.new(0,0,0,0)
    end

    -- Insert the button to the table.
    Buttons[#Buttons+1] = Button
end

I got it from here;

ScriptHelpers

You see the problem is that it would only work for a gui based on offset. My gui is based on scale.

I assume you want to change the CanvasSize of a ScrollingFrame based on its children?

If using a UIListLayout or something similar use UIListLayout.AbsoluteContentSize.Y to get what the offset of the CanvasSize should be.

If not using that, try something like this (I didn’t actually make this code in studio so I apologize if I make a spelling mistake or something):

local scrollF = script.Parent.ScrollingFrame
local childs = scrollF:GetChildren()
local sizeshould = 0
for i = 1, #childs do
sizeshould = sizeshould + childs[i].AbsoluteSize.Y
wait()
end

Notes: scrollF should be your ScrollingFrame
sizeshould is what the offset of CanvasSize should be (you are adding to it each time you go through one of the ScrollingFrame’s children)

As a side note you might want to add an if statement to check if a child is a UI object with AbsoluteSize or use a pcall function.

Hope this helps.