简体   繁体   中英

CGRectMake putting my UIButton in the wrong location

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:

大小检查员

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相关程序错误

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.

Check out how you are reading coordinates wrt origin in IB.

在此输入图像描述在此输入图像描述

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM