Creating Runtime UI anchor problem

So, I have a UI panel, and I need to create UI images inside it, so I created a prefab, that´s properly anchored, but when I run my scrip, it keeps creating the images from the center point, and not from the top left as the used prefab is set

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour {

    public GameObject slotsPrefab;
    public int slotRows;
    public int slotCollumns;
    int x, y;
    float invX, invY;

    // Use this for initialization
    void Start () {
        invX = slotCollumns + 8;
        invY = slotRows + 8;

        for (int i = 1; i < slotRows + 1; i++) {
            for (int j = 1; j < slotCollumns + 1; j++) {
                GameObject slot = (GameObject)Instantiate(slotsPrefab);
                slot.transform.SetParent(this.gameObject.transform);
                slot.GetComponent<RectTransform>().anchoredPosition = new Vector2(0,0);
                slot.GetComponent<RectTransform>().localPosition = new Vector3(x, y, 0);
                x = x + 32;
                if (j == slotCollumns) {
                    x = -130;
                    y = y - 32;
                }
            }
        }
    }
}

well, so I dont know why it keeps creating them from the center… I also want to know how can I set the UI panel height and width… thank you!

Well, I figured it out, it was due to the pivot, so I set it properly and it worked, but I had to make a few changes, and now this is my final code:

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour {

    public GameObject slotsPrefab;
    public int slotRows;
    public int slotCollumns;
    public int slotSpacing;
    int x, y;
    float invWidth, invHeight;

    // Use this for initialization
    void Start () {
        x = 4 + slotSpacing;
        y = -4 - slotSpacing;
        invWidth = ((4+slotSpacing) * 2) + (slotCollumns * 32) + ((slotCollumns - 1) * slotSpacing); //calculates the width
        invHeight = ((4+slotSpacing) * 2) + (slotRows * 32) + ((slotRows - 1) * slotSpacing); // calculates the height
        this.GetComponent<RectTransform>().sizeDelta = new Vector2(invWidth, invHeight); //set the width and height to the panel

        for (int i = 1; i < slotRows + 1; i++) {
            for (int j = 1; j < slotCollumns + 1; j++) {
                GameObject slot = (GameObject)Instantiate(slotsPrefab);
                slot.name = "Slot" + i + "." + j;
                slot.transform.SetParent(this.gameObject.transform);
                slot.GetComponent<RectTransform>().localPosition = new Vector3(x - invWidth, y, 0); //this calculates the accurate position due to the pivot
                x = x + 32 + slotSpacing;
                if (j == slotCollumns) {
                    x = 4 + slotSpacing;
                    y = y - 32 - slotSpacing;
                }
            }
        }
    }
}

So, my hint is mess around with the pivot and see how your changes affect the positioning… in the anchor screen, you can hold shift to change the pivot with the anchor, and hold alt to change the position with the anchor.