Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use an UIAspectRatioConstraint? [SOLVED]

There are no tutorials on this and I cant seem to figure out how to keep the frame onto the screen using this method. I also don't know if I have to use code to do this.

1 answer

✓ Accepted answer

A UIAspectRatioConstraint ensures that a GuiObject maintains a particular aspect ratio even if it’s size is set as a percentage of its parent. If an object with this constraint is also under the control of a UILayout such as UIGridLayout, then the constraint controls the object’s size and overwrites any size the layout would apply.

A UIAspectRatioConstraint can be applied to a GuiObject by parenting it to that object.

When applied to a GuiObject, the UIAspectRatioConstraint will make sure that the object’s ratio is maintained by the value defined in AspectRatio. The AspectType sets what determines the maximum size of the object. When set to FitWithinMaxSize, the constraint will make the object the maximum size it can be within the AbsoluteSize of the element. When set to ScaleWithParentSize, the element’s maximum size will be the size of the parent while still maintaining the aspect ratio. Last, the DominantAxis will determine which axis to use when setting the new size of the element.

--Example 
 GUI.RANDOMGUINAME.AnchorPoint = Vector2.new(0.5,0.5) -- Now it will center values. 
 Therefore...
 GUI.RANDOMGUINAME.Size = UDim2.new(0.5,0,0.5,0) -- This will be half of the screen.
 
 GUI.RANDOMGUINAME.Position = UDim2.new(0.5,0,0.5,0) -- This will center it.

Example of SizeConstraint within your GUI script

local gui = script.Parent
local parent = gui.Parent

local function update()
	gui.SizeConstraint =
		parent.AbsoluteSize.x < parent.AbsoluteSize.y
		and Enum.SizeConstraint.RelativeYY
		 or Enum.SizeConstraint.RelativeXX
end

update()
parent:GetPropertyChangedSignal("AbsoluteSize"):Connect(update)

15 comments

0

Whenever I make the UIAspectRatioConstraint the parent of the frame it makes it go off the screen

0

Have you tried using SizeConstraint? Or does the image need to scale?

0

I will try that

0

The width is supposed to be consistent while the height is not...Are you using it in a single frame, or a frame within another frame?

0

I need the frame to stay in that position even if it has to scale, and the size and scale constraint both went off the screen

0

I am using a single frame

0

Ok, I edited my answer to include a SizeConstraint example

0

I asked because descendants of frames, the width(x) if in offset, will offset all descendants whether they're using scale or not

0

Do I have to make a script for this to work? Otherwise the frame just goes off the screen somewhere when I put it in the constraints

0

I am supposed to make the constraints the parent too right

0

The SizeContrstraint would be part of a script yes...I'm not sure what you want your frame to look like, but an example file is at

0

UIConstraint in the Image Label, UIARC in PlayDockContainer

0

So I make the Sizeconstraint's parent the frame not the frame's parent being the Sizeconstraint

0

I also have an issue where I have explorer open and its on the right of my screen but it doesn't make the game screen more compact

This discussion is preserved as it was. New answers, comments, and votes are closed.