Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

ScrollingFrame with a CanvasSize depending on descendant count? [closed]

Asked by 3 years ago
Edited 3 years ago

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!

Closed as Not Constructive by JesseSong

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
0
Answered by 3 years ago

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

0
ScrollingFrames are not text containers, I just need code to increase the size of the CanvasSzie depending on descendant count. PoWerofThEePg 43 — 3y
Ad
Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
3 years ago

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)