How to overwrite Width and Height values that are driven by a Layout Group in runtime.

I am adding items dynamically into a Layout Group and I cannot set a correct Spacing.

I cannot modify the Width and Height values since they are grayed out and they are driven by Layout Group.

Cause

When UI items are children of a parent GameObject that has a Layout Group component, this component prevents the Width and Height properties of the UI Items from being modified via the Inspector, in order to be driven by the Layout Group in runtime.

Resolution

You can override the Width and Height values by adding a Layout Element component in your item. Next, override automatic values set by the Layout Group with the values that you need.