Mergo
Mergo

Reputation: 82

How do I get my scoll view to fill the entire free space in the vertical layout group in Unity?

I'm developing an application in Unity and I can't get the layout to behave the way I want it to.

enter image description here

The canvas is the red box and the contentWrapper is the green box. I want the green box to adapt to the canvas and leave a fixed size at the top and bottom for the header and footer. That works quite well so far. To make this work, I simply set stretch and stretch for the rect transform of the contentWrapper and specified the spacing at the top and bottom. Then I want the contentWrapper to be split between 2 objects, so I added a vertical layout group to the contentWrapper .

enter image description here

The first object is an picture whose width should be adjusted to the ContentWrapper and thus to the screen. The height should change in such a way that the aspect ratio is retained. I managed that too by activating controll child width in the ContnetWrapper and giving the picture an aspect ratio fitter with "Width Controls Height".

enter image description here

Now we come to my problem: I want a scroll view to completely fill the rest of the ContentWrapper. Depending on which settings I make, I get different results, but I don't get the desired result.

Upvotes: 1

Views: 430

Answers (1)

Mergo
Mergo

Reputation: 82

I solved the issue by giving the picture and the scroll view each a layout element component. I set Flexible Height = 1 for the Scroll View and activated Min Height for the picture and then attached the following script to the Picture:

using UnityEngine;
using UnityEngine.UI;

public class DynamicMinHeightSetter : MonoBehaviour
{
    void Awake()
    {
        GetComponent<LayoutElement>().minHeight = GetComponent<RectTransform>().rect.height;
    }
}

Thanks to derHugo for the Hint with the layout element component.

Upvotes: 2

Related Questions