How to automatically align control to the center of its parent

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By logicor

The document says

SIZE_SHRINK_CENTER = 4 — Tells the parent Container to center the node in itself. It centers the Control based on its bounding box, so it doesn’t work with the fill or expand size flags. Use with size_flags_horizontal and size_flags_vertical.

But somehow it is not working for me. Is there anything besides setting this size_flag required for automatic center alignment to work?

I know how to do it manually in the script but it is kind of a pain in the butt to scripting it for every UI component needs this. Especially when I am using a mix of GDscript and GDNative.

:bust_in_silhouette: Reply From: wombatstampede

I understand that you want to center a control in the editor?
Select “Center” from the layout button (green button above editor window). Which sets the anchor values of the control to 0.5 each.
(Margin values then indicate the distance from each side of the control to the center.)

Alternatively you should be able to set the anchor values to 0.5 in code if you desire.

Thanks, that worked very well!

logicor | 2019-02-25 18:54

:bust_in_silhouette: Reply From: att

You can center control with CenterContainer, Like this
remember to set the size of CenterContainer as extend mode.

Hope to help.