Can ScrollContainer resize itself to its children?

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

I know it may seem like a bit of an odd question, since why would it ever need to do that if it has a scrollbar anyways. But currently I’ve had to make do with a nasty work-around.

My problem is this, I have a scrollcontainer where I only want the scrollbar to appear at a maximum size based on its parent container but if it’s smaller than the maximum size it should just dynamically adjust itself to fit the child nodes. I have tried and failed to find an easy fix for this problem and would like to know if anyone else would happen to have any ideas on the matter?

My biggest problem at the moment is that my work-around isn’t flexible at all, my scrollcontainer lies in a margincontainer and when I change the size of the scrollcontainer to fit its children I’d need to get the max size from a higher up node and adjust it with the margins of the margincontainer. But if I wanted to use it again with another type of container as parent I’d have to fiddle around with it again. Which is kind of bothersome.

:bust_in_silhouette: Reply From: GroundBiscuit

For anyone who’s also having trouble with this, here I have a less hacky way to set a maximum size on the scrollcontainer before the the scrollbar appears. I’ve only implemented it for the y axis but it’s pretty self-explanatory.

export(NodePath) var size_parent
onready var _child_scrollcontainer = get_child(get_child_count()-1)

func _ready():
	_child_scrollcontainer.connect("resized", self, "fit_content")

func _process(delta):

func fit_content():
	var size_parent_node = get_node(size_parent)
	var y_margins = size_parent_node.get_combined_minimum_size().y - get_size().y
	rect_min_size.y = min(_child_scrollcontainer.get_size().y, size_parent_node.get_size().y - y_margins)
1 Like

Thanks for sharing that!