Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: convertRect:toView: doesn't work as I expect

Tags:

ios

frame

cgrect

I'd like to have the coordinates relative to the main window, therefore I'm using convertRect:toView: with nil as second parameter:

CGRect result = [self.view convertRect:addToList.frame toView:nil];

But as a result, I always get

NSRect: {{5, 30}, {35, 35}}

and that are exactly the values I use for generating the addToList-view:

addToList = [UIButton buttonWithType:UIButtonTypeCustom];
addToList.frame = CGRectMake(5.0f, 30.0f, 35.0f, 35.0f);

If I'm adding the addToList-button to a view which is placed in the bottom right corner of the screen, I would expect a CGRect like for example 950, 700, 35, 35 on an iPad landscape, because those are the coordinates of the button relative to the topmost view.

First, I did the conversion in the initWithFrame:-method of a UIViewController; but now, I'm doing it afterwards... what am I missing?

Thanks!

EDIT: is it important that "self.view" is inside a UIScrollView?

like image 235
swalkner Avatar asked Jun 07 '11 07:06

swalkner


1 Answers

If you look at convertRect:toView:'s documentation, you will see that the rect that you pass is defined as within the bounds of the view on which you are calling the method. Since self.view presumably takes the entire window, the rect doesn't change with respect to the window. You should message the parent of the button to get the actual location of the button w.r.t the window.

like image 60
Deepak Danduprolu Avatar answered Sep 17 '22 11:09

Deepak Danduprolu