Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make textField fit scroll view and also expand?

Discussion in 'UI Toolkit' started by CDF, Dec 8, 2022.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,306
    I'm trying to make a textField fit the size of the parent scroll view but also have that scroll view expand the contents if the textField gets too large.

    When using style.flexGrow = 1, the textField fits the scrollView size, but scroll view doesn't expand once the textField gets too large:
    textField_flexGrow.gif

    When not using flexGrow the scroll view expands correctly, but textField no longer fills the space:
    textField_noFlexGrow.gif

    Code (CSharp):
    1. textField = new TextField() { multiline = true };
    2. textField.style.whiteSpace = WhiteSpace.NoWrap;
    3. textField.style.flexGrow = 1;
    4. textField.style.marginLeft = textField.style.marginRight = textField.style.marginTop = textField.style.marginBottom = 5;
    5.  
    6. scrollView = new ScrollView(ScrollViewMode.VerticalAndHorizontal);
    7. scrollView.style.flexGrow = 1;
    8. scrollView.Add(textField);
    Any ideas how to solve this? Using Unity 2021.3.5
     
  2. Neutron

    Neutron

    Joined:
    Apr 25, 2011
    Posts:
    45
    Try setting the MinHeight of your textfield to match the height of the scroll view