Grid of buttons in Unity – button bounds are stretching beyond their visuals, capturing clicks meant for other buttons

Grid of buttons in Unity – button bounds are stretching beyond their visuals, capturing clicks meant for other buttons

I am using unity UI for the first time and trying to create a level selection menu just like Angry Birds. 
I had successfully created the dynamic level selection menu. It was all good in the inspector but when I build and run it, something strange happens: when I click on the first button of the first row, instead of that button, the last button of that row got clicked. 
I searched and found that the image was not stretching (because of preserve aspect property checked) but the bounds were stretching with the resolution so the bounds were overlaping and the wrong button was getting clicked.

There is a panel which is child of the canvas gameObject.
Buttons are child of panel gameObject.
Panel's anchors are set to stretch the canvas.
Buttons' anchors are set to stretch the panel.

Here is the code to create menu dynamically.
public class UITest : MonoBehaviour 
{

    public Button button;
    public RectTransform parent;

    void Start()
    {
        SetupButtons();
    }


    void SetupButtons()
    {
        float x = -155;
        float y = 55;

        for(int i=1; i<15; i++)
        {
            var I = i;
            var btn = Instantiate(button) as Button;
            var btnRect = btn.GetComponent();

            float width = btnRect.rect.width/5;
            float height = btnRect.rect.height/5;

            btn.transform.SetParent(parent, false);
            btnRect.anchoredPosition = new Vector2(x, y);
            btn.onClick.AddListener(() => Temp(btn, I));

            if(i%5 == 0)
            {
                y += (height);
                x = -155f;
            }

            else
            {
                x += -width;
            }

            //Debug.Log(width +", " + height);
        }
    }

    void Temp(Button btn, int i)
    {
        //Debug.Log("Clicked: " + i);
        Debug.Log(btn.GetComponent().anchoredPosition + " " + btn.GetComponent().rect);
    }
}

Here are some screenshots to better understand the problem. 
I hope you guys have understood the problem please help me solve the problem.


Solutions/Answers:

Answer 1:

transform.anchorMin = new Vector2(0f, 0f);
transform.anchorMax = new Vector2(1f, 1f);
transform.pivot = new Vector2(.5f, .5f);

http://answers.unity3d.com/questions/1007886/how-to-set-the-new-unity-ui-rect-transform-anchor.html

Answer 2:

Thank you everyone for the response but I had solved the problem myself.
In case anyone else facing this problem, I just set the anchor points to be the middle of the panel and my problem vanished.

References