Search Unity

Scroll view does not resize

Discussion in 'UIElements' started by weltraumaffe, Jun 5, 2019.

  1. weltraumaffe

    weltraumaffe

    Joined:
    Nov 13, 2012
    Posts:
    4
    Hey

    I have a problem when adding elements to a scroll view with absolute positioning
    The horizontal scrollbar does not appear and the vertical scroll bar only scrolls not far enough down.
    How can I update the size of the content container correctly?
    The code I used is below.

    Thank you,

    Peter

    Code (CSharp):
    1. VisualElement root = rootVisualElement;
    2.  
    3. var scroll = new ScrollView(ScrollViewMode.VerticalAndHorizontal);
    4.  
    5. for (int i = 0; i < 100; i++)
    6. {
    7.     Label element = new Label(i.ToString());
    8.     element.style.left = i * 10;
    9.     element.style.top = i * 10;
    10.     element.style.right = (i+1) * 10;
    11.     element.style.bottom = (i+1) * 10;
    12.     scroll.contentContainer.Add(element);
    13. }
    14.  
    15. root.Add(scroll);
     
  2. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    24
    Hi weltraumaffe! Unless style.position is set to Position.Absolute, you are not using absolute positioning. Were you in absolute positioning, it wouldn't work since only top, left, width and height would matter, and also, you would have to manually size the contentContainer. Could you add a picture of what you want to achieve visually?
     
    weltraumaffe likes this.
  3. weltraumaffe

    weltraumaffe

    Joined:
    Nov 13, 2012
    Posts:
    4
    Here is the current look.
    As you can see the horizontal scroll bar is not visible and the vertical scroll bar stops too early.
    I want the scroll bars to correctly show all items.
    When setting the positioning to absolute it just changes the reference or did I misunderstand this from the description. Screenshot 2019-06-05 at 20.54.38.png

    My goal is to create a scroll view for a hexagon map editor. It should contain an arbitrary amount of hexagons and scroll to all ends.
     
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    24
    Relative/Absolute positioning affects the way layout is performed. In relative positioning, siblings may impact the resulting layout, while it's not the case for absolute. Try the following:
    -Use absolute positioning
    -Assign only top, left, width and height
    -Compute the width/height of the content container manually and set it explicitly
     
    weltraumaffe likes this.
  5. weltraumaffe

    weltraumaffe

    Joined:
    Nov 13, 2012
    Posts:
    4
    Thank you that works :)
     
    AlexandreT-unity likes this.
  6. mrSaig

    mrSaig

    Joined:
    Jan 14, 2010
    Posts:
    30
    Is there a way to set the ScrollViewMode any other way than in the constructor? ... I used Scrollview with UXML and didn't find a way to set the mode