IPhone the correct coordinates of the landscape window

I am trying to get the coordinates of a table view window using the following code:

[self.tableView.superview convertRect:self.tableView.frame toView:nil]

It reports the correct coordinates in portrait mode, but when I turn to the album, it no longer reports the correct coordinates. First, it flips the x, y coordinates, as well as the width and height. But it's not a problem. The real problem is that the coordinates are wrong. In the portrait, the window coordinates for the table view are {{0, 114}, {320, 322}}, and in the landscape the window coordinates are {{32, 0}, {204, 480}}. Obviously, the value of x is wrong here, right? Shouldn't it be 84? I am looking for a fix for this problem, and if anyone knows how to get the correct view coordinate windows in landscape mode, I would really appreciate if you shared this knowledge with me.

Here are some screenshots so you can see the layout of the view.

Portrait: http://i.stack.imgur.com/IaKJc.png

Landscape: http://i.stack.imgur.com/JHUV6.png

+8
iphone window coordinates landscape
source share
5 answers

I found what I believe was the beginning of a solution. It seems that the coordinates that you and I see are based on the lower left or upper right corner, depending on whether the orientation is UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft.

I don’t know why yet, but hopefully this helps. :)

[UPDATE] Therefore, I assume that the start of the window is 0.0 in normal portrait mode and rotates using ipad / iphone.

So, this is how I solved it.

First, I grab my orientation, the borders of the windows and spin my look in the window (with insidious coordinates)

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; CGRect windowRect = appDelegate.window.bounds; CGRect viewRectAbsolute = [self.guestEntryTableView convertRect:self.guestEntryTableView.bounds toView:nil]; 

Then, if the orientation is landscape, I change the x and y coordinates, and the width and height

 if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) { windowRect = XYWidthHeightRectSwap(windowRect); viewRectAbsolute = XYWidthHeightRectSwap(viewRectAbsolute); } 

Then I call my function to fix the origin based on the upper left corner, regardless of the rotation of the ipad / iphone. It captures the origin depending on where the current 0.0 resides (depending on orientation)

 viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height); 

Here are two functions that I use.

 CGRect XYWidthHeightRectSwap(CGRect rect) { CGRect newRect; newRect.origin.x = rect.origin.y; newRect.origin.y = rect.origin.x; newRect.size.width = rect.size.height; newRect.size.height = rect.size.width; return newRect; } CGRect FixOriginRotation(CGRect rect, UIInterfaceOrientation orientation, int parentWidth, int parentHeight) { CGRect newRect; switch(orientation) { case UIInterfaceOrientationLandscapeLeft: newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), rect.origin.y, rect.size.width, rect.size.height); break; case UIInterfaceOrientationLandscapeRight: newRect = CGRectMake(rect.origin.x, parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height); break; case UIInterfaceOrientationPortrait: newRect = rect; break; case UIInterfaceOrientationPortraitUpsideDown: newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height); break; } return newRect; } 
+6
source share

This is a hack, but it works for me:

 UIView *toView = [UIApplication sharedApplication].keyWindow.rootViewController.view; [self.tableView convertRect:self.tableView.bounds toView:toView]; 

I am not sure if this is the best solution. It does not work reliably if your root view controller does not support the same orientations as the current controller.

+3
source share

You should be able to get the current coordinates of the table view from self.tableView.bounds

0
source share

Your code should be:

 [tableView convertRect:tableView.bounds toView:[UIApplication sharedApplication].keyWindow]; 

This will give you a view rectangle in the window coordinate system. Be sure to use a "border" rather than a "border". frame is the view rectangle in its coordinate system of the parent view. "bounds" is a representation rectangle in its own system. Thus, the above code requests a table view for converting your own rectangle from your system to the window system. Your previous code requested a view of the parent table to convert the table rectangle from the parent coordinate system to nothing.

0
source share

Try using frames instead of self.parentViewController.view.bounds frames as it gives me customized coordinates according to the current orientation

0
source share

All Articles