fit UI prefab to screen

I need to instantiate a UI prefab and scale it to the screen size. I have tried:

                modalInst.GetComponent<RectTransform>().anchorMin = new Vector2(0, 0);
                modalInst.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1);
                modalInst.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0.5f);
                modalInst.GetComponent<RectTransform>().offsetMin = new Vector2(0, 0);
                modalInst.GetComponent<RectTransform>().offsetMax = new Vector2(0, 0);

This does not work. There must be a simple way to do this that I am overlooking.

You want to use Canvas Scaler component on your camera game object.
If your game is a horizontal game, adjust Match to 1 based on Height. This basically means that the scale of everything will be adjusted based on the resolution of height, which won’t change across different devices for a horizontal game.
If you want a vertical game, then change Match to 1 based on Width.