3

I created a new project as Single View Application. Added code in view controller for converting a view coordinate to window coordinate:

- (void) dumpFrame
{
    CGRect bounds = self.view.bounds;

    // UIView* rootView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
    // I tried rootView instead of nil and self.view.superview instead of self.view but got {0,0} coordinate

    CGPoint newCoord = [self.view convertPoint: bounds.origin
                                        toView: nil];

    bounds.origin.x = newCoord.x;
    bounds.origin.y = newCoord.y;
    NSLog(@"view.frame=%@", NSStringFromCGRect(bounds));
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation: fromInterfaceOrientation];
    NSLog(@"===>didRotate");
    [self dumpFrame];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];
    NSLog(@"===>willRotate");
    [self dumpFrame];
}

My log:

===>wilRotate
view.frame={{0, 20}, {768, 1004}}
===>didRotate
view.frame={{748, 0}, {1024, 748}}

Instead of

{{0, 20}, {1024, 748}}

I got

{{748, 0}, {1024, 748}}

after rotation. What do I need to do to get {0,20}? I will be glad any help.

stosha
  • 1,935
  • 2
  • 24
  • 26

2 Answers2

3

Apparently the UIWindow's coordinate system always remains in portrait orientation, and the rotation is applied simply by setting the transform property of the UIWindow's root VC. Got this information from this SO question.

At least this explains why you get the unexpected coordinates. Unfortunately, however, I have been unable to find a way how to actually solve your problem. I assume there should be a way to make use of the transformation matrix that is in effect, but my math skills are not up to this. Maybe someone else can step in here.

EDIT

Maybe you can do something with this code (in the GitHub repo of the "iOS Developer's Cookbook"). Parts of the code are explained and presented with illustrations in this article.

Community
  • 1
  • 1
herzbube
  • 12,320
  • 8
  • 42
  • 76
  • Adding a comment here for people of the future: In iOS 8 onwards the coordinate system matches the orientation, and isn't always in portrait, so many of the calculations don't have to be performed. – Rizwan Sattar Jan 23 '15 at 00:17
-1

try:

CGPoint newCoord = [view.superview convertPoint: bounds.origin
                               toView: nil];

origin point of a view is relative to its superview, so you have to convert the point from the superview coordinate system

meronix
  • 6,182
  • 1
  • 21
  • 35
  • Tried. newCoord always equals {0, 0} – stosha Jul 02 '13 at 14:30
  • For rootView (see my question) I got {0,0} for newCoord too. – stosha Jul 02 '13 at 14:33
  • -1 because only frame coordinates are relative to the superview. Bounds coordinates, however, are in the view's own coordinate system. I will be happy to remove the downvote if you edit your answer. – herzbube Jul 05 '13 at 14:14