Adaptable ScrollContainer size on UI

Godot Version

v4.5.stable.official [876b29033]

Question

I have a ScrollContainer that begins empty but will have child nodes added at runtime.

I want the ScrollContainer’s height (size.y) to behave like this:

  • When it has no children, its height should be 0. (Can be done by setting visibility)

  • When children are added, its height should match the total height of all children combined.

  • If the total child height exceeds a maximum height I define, the container should cap at that max height and then show the scrollbar.

Any suggestions how to achieve that?

ScrollContainer should only have one child and its size.y should be the desired max height. If you add a VBoxContainer as a child of the ScrollContainer and add the other nodes as children of the VBoxContainer, it should work as you described. Although, you may have to adjust some size flags.

1 Like

Yes, I actually do that, but for some reason forgot to mention it :smiley: My bad!

But it still doesn’t solve my issue because sometimes the auto-sizing with containers, and changing min size doesn’t work properly together. I think I need to figure out what happens when exactly in a container. Maybe sort_children or pre_sort_children signal can help.

I’ll send more details once I’m back checking that. I appriciate the response!

So the scroll container is inside another container and it needs to update custom_minimum_size based on its contents? If that’s the case, I just tested and confirmed the following solution.

func _ready() -> void:
    inner_vbox.resized.connect(resize_scroll)

func resize_scroll() -> void:
    scroll.custom_minimum_size.y = minf(inner_vbox.size.y, MAX_HEIGHT)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.