3

When I use CGRectMake in my program, it behaves very strangely. In Interface Builder, my button has coordinates of (260, 388) and is always in portrait mode. When I put in the following code (just to experiment), which should keep it in the same place, the X value is fine but the Y value puts it in a spot more like 300.

self.theButton.frame = CGRectMake(260, 388, 120, 120);

Are there some quirks in CGRectMake that cause the coordinates to get screwy? No other lines of code affect the size or shape of this button and when I comment out this line the button is in the correct place from IB.

EDIT: Autolayout is not on. EDIT: The problem seems to be that my code's concept of the frame's Y value and IB's concept don't match up.

Buttons 1.1 through 3.2 are listed here in their order from top to bottom. The first number after the button number is their (correct) Y value according to IB. The second number is their Y value according to an NSLog's button.frame.origin.y. The last number is the difference. The difference grows exponentially, by 50 or 51 each time. This is clearly the problem, I'm setting my CGRectMake using IB's y value system but the code is using some other numbers for the Y value. Any ideas as to why?

Button -> IB Y-value -> NSLog Y-Value -> Difference

1.1 -> 4 -> 5 -> 1

1.2 -> 132 -> 184 -> 52

1.3 -> 260 -> 362 -> 102

3.1 -> 388 -> 541 -> 153

3.2 -> 516 -> 719 -> 203

EDIT: My size inspector:

Size Inspector

The first image here is part of my IB (correct). The second image is when I run the program, you see the one Item button overlaps the magnifying glass (ignore the low alpha).

IB correcdt Program incorrect

EDIT: After further experimenting, it appears that using CGRect to move UIViews works totally fine EXCEPT when using it in viewDidLoad. I moved the code to viewDidAppear and it now works. No idea why this happens but there it is. Closing this question out.

robertfiorentino
  • 584
  • 7
  • 20
  • Does a nav bar appear when you actually run the app? (The bar at the top with the time and number of bars of service you have, etc.) – Albert Renshaw Jul 06 '13 at 01:53
  • Is the size inspector that you show a picture of from one of the buttons that moves, or the view that contains them, or one of the top left buttons that stays in the same place? – lnafziger Jul 06 '13 at 03:55
  • @AlbertRenshaw no nav bar or status bar in IB or when it runs. – robertfiorentino Jul 06 '13 at 14:36
  • @Inafziger the size inspector I showed is the button that moves. The size inspector for the view is basically the same but the the autosize struts (I think that's what they're called) are left and top rather than left and right. All other buttons are the same as the picture I posted. – robertfiorentino Jul 06 '13 at 14:38
  • @fiorgodx I would do a search and find (cmd+f) in your .m file to see if there is anywhere else that you are moving the button `.center = CGPointMake` ... `.frame = CGRectMake` ... and just `self.theButton` – Albert Renshaw Jul 06 '13 at 17:32
  • @fiorgodx Also, in addition the the thing I just posted... sometimes flukes really do just happen. Try doing a clean on the project, then quitting and relaunching xcode (and the simulator) then re-running the app. – Albert Renshaw Jul 06 '13 at 17:34
  • Third idea: Is the "item" graphic the same that is used in all of the other buttons? Because if it's a different image, the image itself may have a gap above it, and you might be using the wrong thing to display it (i.e. button background vs button graphic) – Albert Renshaw Jul 06 '13 at 17:35
  • Fourth: If CGRectMake just for whatever reason is being crazy for your app you could always try `self.theButton.center = CGPointMake(x,y);` (*Note: that sets the center of the button, not the upper left corner like CGRectMake does) – Albert Renshaw Jul 06 '13 at 17:36
  • One thing that could really be happening (other than the 4 posts above this) is that you have the x and y position for your main view (which is the size of the iPhone screen) but you are adding the button to a different view that isn't the size of the iPhone screen, therefor a y-value of 50 on the main view would be 50 pixels down from the top of the screen... but a y-value of 50 if you are adding the button to some random UIView could be any number of pixels down from the top of the screen. How are you adding the button to the screen? `[self.view addSubview` or [`randomCustomView addSubview`? – Albert Renshaw Jul 06 '13 at 17:39
  • @AlbertRenshaw It could be one of those things but I know for sure that moving the CGRectMake code from viewDidLoad to viewDidAppear fixed the problem (see my answer). I'm not sure if this is a quirk in viewDidLoad or if it was one of the reasons you put above, but I'm considering this solved :D – robertfiorentino Jul 07 '13 at 01:35
  • @fiorgodx Oh! Then surely you were using something along the lines of `self.view.frame.size.height` in your CGRectMake code... in `ViewDidLoad` the view's frame's height is whatever it is by default in your xib... in `viewDidAppear` the view's frame's height is whatever the device is... so if your xib has an iPhone5 view and you are running the app on an iPhone4 (or vice-versa) the button will be in a different location because the iPhone screen changes values. – Albert Renshaw Jul 07 '13 at 05:34
  • If you want the screen height of the actual device (the one you get in `viewDidAppear`) but you want to put your code in `viewDidLoad` than instead of using `self.view.frame.size.height` use `[[UIScreen mainScreen] bounds].size.height` – Albert Renshaw Jul 07 '13 at 05:36

4 Answers4

0

Check out how you are reading coordinates w.r.t origin in IB.

enter image description here enter image description here

NightFury
  • 13,436
  • 6
  • 71
  • 120
0

I had the same problem. Try turning off Autolayout. It worked for me!

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
0

Since each of your buttons is progressively further down the screen, it looks like "Auto-resize subviews" is selected on the view that contains your buttons, and is stretching to a larger height.

This is probably because in IB you are laying out on an iPhone 4 and the simulator/actual device that you are testing on is an iPhone 5. There are other explanations for the view changing sizes, but this is pretty likely.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • This is on iPad by the way. And the thing is, IB isn't moving the views to the wrong spot, everything looks good when the program loads up. It's not until I programatically change a button's position that things look out of place. The problem is how the program (via NSLog) is interpreting the coordinates. In IB it looks great, the program looks good when it runs, but a button I know to be at y value 388 the program tells me is at y value 541. It says the bounds of the window is 1024x768, but when I check the y value of a button at origin y = 768, it says it's y is 1071. – robertfiorentino Jul 05 '13 at 17:16
  • Are you in the same orientation in IB and on the iPad? Ie, both landscape or not portrait? – lnafziger Jul 05 '13 at 19:26
0

After further experimenting, it appears that using CGRect to move UIViews works totally fine EXCEPT when using it in viewDidLoad. I moved the code to viewDidAppear and it now works. No idea why this happens but there it is. Closing this question out.

robertfiorentino
  • 584
  • 7
  • 20