5

I know convertRect:toRect is the key here, but I'm not sure how to call it. I have a subview of my cell, and I want its position in the app's entire window.

I need to know this when I tap on this button (the subview) in its target action method. I need to call the convertRect:toRect function on the cell, I figure, in order to get the right coordinates, but in this method I have no reference to the cell itself.

Do I climb the superview hierarchy? That seems gross, as I'm not totally sure what I would get, as it's embedded in the contentView of the cell, and Apple does wacky stuff with their private subviews and whatnot.

Code:

@IBAction buttonPressed(sender: UIButton) {
    // This button got the callback that it was pressed. It's in a cell. I need to convert its rect to that of the window here. I need the coordinate system it's calculated from (the cell) which I'm not sure of how to easily do in a target action callback.
}
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • possible duplicate of [iPhone - Get Position of UIView within entire UIWindow](http://stackoverflow.com/questions/1465394/iphone-get-position-of-uiview-within-entire-uiwindow) – Lyndsey Scott Dec 22 '14 at 04:30
  • @LyndseyScott No, this is a specific case in the context of cell subviews, where it's not straightforward to get the reference view of the view. – Doug Smith Dec 22 '14 at 04:31
  • 1
    Why do you think that? – Lyndsey Scott Dec 22 '14 at 04:32
  • @LyndseyScott Because it's the very issue I'm struggling with? If in the above situation it's trivial to get reference to the cell the subview is contained in, share your solution. – Doug Smith Dec 22 '14 at 04:37
  • I agree with rmaddy's solution. You don't need a reference to the cell... just the button. You don't need to convert the button to the cell's coordinate system then convert that to the window's. It can be done in one step. – Lyndsey Scott Dec 22 '14 at 04:38
  • If you want the coordinates of the button relative to the main window then it is as simple as the code in my answer. – rmaddy Dec 22 '14 at 04:40
  • @LyndseyScott When I pass the button, the resulting rect is incorrect. It's only correct when I climb the superview tree until I find the cell. – Doug Smith Dec 22 '14 at 04:40
  • @rmaddy I tried the code in your solution. It simply doesn't work. – Doug Smith Dec 22 '14 at 04:41
  • Define "doesn't work". You've posted enough question here to know that those are meaningless words. – rmaddy Dec 22 '14 at 04:41
  • I thought you'd read the reply above. "When I pass the button, the resulting rect is incorrect." – Doug Smith Dec 22 '14 at 04:42
  • How incorrect is it? Try passing `self.frame` instead of `self.bound`. Does that fix it? – rmaddy Dec 22 '14 at 04:42
  • No. From [this question and the corresponding answer](http://stackoverflow.com/a/3034886/998117) I can't help but think I need the cell itself. – Doug Smith Dec 22 '14 at 04:44
  • Do this - take a screenshot with your button in place. Log the button's frame and the converted frame. Measure the actual position of the button in the screenshot (in points, not pixels). Update your question with all of this info. – rmaddy Dec 22 '14 at 04:46
  • The delta? It's off about 50pts on the y axis, seemingly the vertical distance of the button from the top of the cell. – Doug Smith Dec 22 '14 at 04:47
  • @DougSmith There was a typo in my answer. rdelmar noticed it. See the update. It should fix the delta you were seeing. – rmaddy Dec 23 '14 at 00:29

2 Answers2

10

It's actually very simple:

CGRect windowRect = [someSubview convertRect:someSubview.bounds toView:nil];

Assuming you have a button handler:

- (void)someButtonHandler:(id)sender {
    UIButton *button = sender;

    // do your stuff

    CGRect windowRect = [button convertRect:button.bounds toView:nil];
}

Of course it's even easier if your method is setup as:

- (void)someButtonHandler:(UIButton *)button {
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • How do I get `someSubview` in this case outlined in the question? – Doug Smith Dec 22 '14 at 04:32
  • That's up to you. Your question makes it sound like you already have the subview. If not, clarify your question because it is unclear what you have. – rmaddy Dec 22 '14 at 04:33
  • I do have the subview and I thought the question made that clear. The subview is in a cell. Calling the `convertRect` code on the subview doesn't present the correct result, as its coordinate system is in terms of the cell it's contained in. – Doug Smith Dec 22 '14 at 04:35
  • Update your question with the actual code where you want to convert the subview's frame. – rmaddy Dec 22 '14 at 04:36
  • Done. I don't think it's much different than the situation I've outlined, but I hope it helps. – Doug Smith Dec 22 '14 at 04:39
  • What is "self" in your code? Shouldn't that be button? – rdelmar Dec 23 '14 at 00:26
  • You still have one more "self" in the first line. Anyway, thanks for clearing up a misconception that I've apparently had all a long, that the receiver had to be the superview of the view whose rect you were converting. This might be the misunderstanding @DougSmith has with this answer too, since he thinks he needs to get a reference to the cell. – rdelmar Dec 23 '14 at 01:05
  • @rdelmar Fixed the other `self`. Thanks. Note that `[button convertRect:button.bounds toView:someOtherView]` is the same as `[button.superview convertRect:button.frame toView:someOtherView]`. – rmaddy Dec 23 '14 at 01:09
  • Yeah, I was just testing that to make sure I understood what I had been doing, and why it gave me the same result as the way you're doing it. – rdelmar Dec 23 '14 at 01:15
0

Swift Solution
converting myView's frame to the corresponding frame in the parentView

let myViewGlobalFrame = myView.convert(myView.frame, to: parentView)
Deitsch
  • 1,610
  • 14
  • 28