0

I'm trying to position a button.

Here is the code for the positioning..

[btnAbs setFrame:CGRectMake(57, 50, 106, 99)];

The coordinates I got are from here:

enter image description here

As you can see the xib stats the x & y to be at 57 and 192, which is where I want the button to be.

However when I run it in simulator, here is where its placed:

enter image description here

Obviously i could keep guessing and guessing the x and y coordinates, but this is very time consuming. So how come it's doing this? Please join the links together when looking at the pics as i need more than 10 reps to post images, or a mod fix this please?

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Kee
  • 45
  • 1
  • 3
  • 10

2 Answers2

2

The problem is here:

Interface Builder view settings

The “origin” in Interface Builder doesn’t actually affect how the view gets positioned programmatically—it’s just a visual aid. If you click the dot in the top left of that box, the X and Y coordinates will change to the top-left of the view, which are the coordinates you want to pass to -setFrame:.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • Its weird, but it actually does affect it. I have noticed many times when using the GUI builder that things dont come out where I think they should because I have selected a different alignment mechanism within IB. If you look at his image in IB, placement shows it only a couple pixels from the left, yet his x Coord. is 57 = ~ 106 / 2. In IB it places your objects based upon your setting, but in code it ONLY does it by top-left. Obviously you are correct that it doest however effect it programatically :-) – trumpetlicks Feb 19 '13 at 18:26
1

It looks to me as if you have the GUI designer aligning base upon the center center of your image view. When you do it in code, it is going to align based upon the top left of the image view.

Further, your code places it at a y of 50, where your GUI designer is showing a y coord. of 192.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • +1. Set your alignment to top-left in IB and you will get the correct coordinates to input to setFrame: – Taum Feb 19 '13 at 18:22