Why is my scroll view's content container not large enough?

I’ve got a scroll view inside an editor window. I could not for the life of me get the horizontal scroll bar to show up - if I set showHorizontal to true, I just got a disabled scroll bar.

After doing battle with uss and cross-referencing a different window where the scroll bar does show up for a while, my solution was:

var contentContainer = scrollView.Q("unity-content-container");
contentContainer.style.minWidth = requiredWidth;
contentContainer.style.minHeight = requiredHeight;

But that seems like an absurd hack. I also have to update it manually to fit the elements.

The only difference I can find between the working window and my window is that the first one has the scrolled area below the rest of the UI, while my window has it on the right.

Am I running into a bug? If I have a scrollView, and I add VisualElements to it, the scroll view’s content container should automatically stretch to fit the contents so the scrolling can scroll, right? Assuming that I haven’t added any classes or messed with the styles of the scroll view.

The contents are just rows set to flex-direction: row, and squares with min-width and max-width inside those rows.

Have you tried changing the ScrollView mode?

6855275--798740--scrollview_mode.png

<ui:ScrollView mode="VerticalAndHorizontal"/>

That did the trick. Thanks!

I’m creating these through C# (builder’s still unusable), and I was trying to set scrollView.showHorizontal = true, which apparently doesn’t do the same thing.

Is there any reason why we can’t set the Mode from code after we’ve created a ScrollView? I’m using some utilities I wrote in order to be able to write:

var scrollView = container.CreateChild<ScrollView>("some-class", "some-class-2");

instead of:

var scrollView = new ScrollView();
scrollView.AddToClassList("some-class");
scrollView.AddToClassList("some-class-2");
container.Add(scrollView);

The problem is that that relies on calling the default constructor, and it seems that I’m stuck with the default mode. I’d love to just be able to call SetScrollViewMode, but that’s internal.

I’ll talk to the team about exposing it, but borrowing directly from what this method does, I think this should work:

scrollView.RemoveFromClassList(ScrollView.verticalVariantUssClassName);
scrollView.AddToClassList(ScrollView.verticalHorizontalVariantUssClassName);
1 Like

Oh and “showHorizontal” is different in that it forces the visibility of the scroller even if there is no overflow.
Whereas the mode changes how the ScrollView reacts to the size of its children.

@antoine-unity Hi! I’m struggling with the same thing. I changed the ScrollView to VerticalAndHorizontal, but when the content inside the scroll view expands (I’m adding new elements to a flex element), ScrollView doesn’t update the scroll bars. Is there some trick to it? Currently I’m trying to re-set the unity-content-container width with the same width it has in GeometryChangedEvent, but this doesn’t always trigger either in the parent container. Any tips on what might cause the ScrollView not detecting the content change?