PDA

View Full Version : Frame of UIView occasionally not placed where specified




99miles
May 2, 2009, 09:35 PM
I'm creating a bunch of UIViews and immediately adding them to an array which is constantly looped through to animate the views. All the views are created with a frame where frame.origin.y = -45. But on occasion they get placed with a y of 0. I can't figure out for the life of me why that is.

1)I override setFrame in my ViewController and added an NSLog to that to make sure it's not getting called from some unexpected location. This gets called as soon as I create the view and set the frame. It outputs the correct y value of -45.

2) Then I set the frame of the object I trace out the frame and it is also correct:


[tvc.view setFrame:frame];
NSLog(@"%f %f", tvc.view.frame.origin.x, tvc.view.frame.origin.y);
NSLog(@"%f %f", tvc.view.frame.origin.x, tvc.view.frame.origin.y);


..and that outputs say:
176.000000 -45.000000
176.000000 -45.000000

That's what I wanted!

3) But in the animation loop when I output the y value I get 0.00:


CGRect frame = [[[tv layer] presentationLayer] frame];
NSLog(@"do step -- %@ %f", tvc.letter, frame.origin.y);


Is there something I can call to make sure that the view gets drawn to the correct place? It looks like the default for a frame.origin.y value is 0, so I'm assuming this just isn't getting updated quickly enough or something. I set the following in the view but it didn't help:
[self setContentMode:UIViewContentModeRedraw];

Again, 90% of this time, this works like a charm, but every once in a while a view is created in the wrong location. The biggest problem is that when this happens my hitTest doesn't detect the touch of that view. I'm not exactly sure why that is, but I know that it is a result of the view not created created in the correct place, so the coordinates somehow get off.

Thanks in advance for any ideas, clues, or solutions!



sujithkrishnan
May 2, 2009, 11:48 PM
I'm creating a bunch of UIViews and immediately adding them to an array which is constantly looped through to animate the views. All the views are created with a frame where frame.origin.y = -45. But on occasion they get placed with a y of 0. I can't figure out for the life of me why that is.

1)I override setFrame in my ViewController and added an NSLog to that to make sure it's not getting called from some unexpected location. This gets called as soon as I create the view and set the frame. It outputs the correct y value of -45.

2) Then I set the frame of the object I trace out the frame and it is also correct:


[tvc.view setFrame:frame];
NSLog(@"%f %f", tvc.view.frame.origin.x, tvc.view.frame.origin.y);
NSLog(@"%f %f", tvc.view.frame.origin.x, tvc.view.frame.origin.y);


..and that outputs say:
176.000000 -45.000000
176.000000 -45.000000

That's what I wanted!

3) But in the animation loop when I output the y value I get 0.00:


CGRect frame = [[[tv layer] presentationLayer] frame];
NSLog(@"do step -- %@ %f", tvc.letter, frame.origin.y);


Is there something I can call to make sure that the view gets drawn to the correct place? It looks like the default for a frame.origin.y value is 0, so I'm assuming this just isn't getting updated quickly enough or something. I set the following in the view but it didn't help:
[self setContentMode:UIViewContentModeRedraw];

Again, 90% of this time, this works like a charm, but every once in a while a view is created in the wrong location. The biggest problem is that when this happens my hitTest doesn't detect the touch of that view. I'm not exactly sure why that is, but I know that it is a result of the view not created created in the correct place, so the coordinates somehow get off.

Thanks in advance for any ideas, clues, or solutions!

i guess that you are printing the frame values in soem loadView or some methods which will load/change the UIview. Or maybe in some animation methods...

Better put this NSLog in viewDidApeear and see the frame values...

99miles
May 3, 2009, 12:24 AM
I have the NSLog in viewDidLoad and it says -45 but it's not. So weird.

99miles
May 3, 2009, 12:49 AM
Ahah, I got a hint, but still not sure why this is happening.
In viewDidLoad I get the frame of the view it's -45, but if I get the frame of the views presentationLayer it says 0. Any ideas?
The views that behave correctly return -45 for each.

99miles
May 3, 2009, 11:58 AM
I'm talking to myself for a moment here, but I was thrown off in my last post by what must be an sdk bug. It turns out that the following NSLogs output different values even though they are essentially the same code:

CGRect tvFrame = [[[tv layer] presentationLayer] frame];
NSLog(@"do step -- %@ %f", tvc.letter, [[[tv layer] presentationLayer] frame].origin.y); // outputs 0
NSLog(@"do step -- %@ %f", tvc.letter, tvFrame.origin.y); // outputs -45

SO what I have noticed is that both the frame of the view and the frame of the presentationLayer have a y value of -45 in viewDidLoad, but when the animation loop looks at them a moment later, it says the presentationLayer frame has a y value of 0 while the frame has a y value of -45. Thoughts?

p.s. i didn't put the nslog in viewDidAppear b/c I can't get it to get called when i don't use initWithNib. Not sure if it ever does get called in a case like this. Seems like viewDidLoad is sufficient (?)

PhoneyDeveloper
May 3, 2009, 05:35 PM
What happens if you try this

NSLog(@"do step -- %@ %f", tvc.letter, (float)[[[tv layer] presentationLayer] frame].origin.y); // outputs 0

varargs functions may not do what you expect sometimes.

99miles
May 3, 2009, 06:25 PM
I'll give it a shot but I seem to have made the problem go away by simply referencing the view.frame before getting the presentationLayer frame.
It doesn't make sense why it works but it seems to force the draw presentationLayer coordinates to update properly.
Thanks!