iPhone correct landscape window coordinates

前端 未结 5 1058
终归单人心
终归单人心 2021-02-15 15:31

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

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

相关标签:
5条回答
  • 2021-02-15 15:53

    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 this is the best solution. It may not work reliably if your root view controller doesn't support the same orientations as the current view controller.

    0 讨论(0)
  • 2021-02-15 15:56

    Try bounds instead of frame self.parentViewController.view.bounds for it gives me adjusted coords according to the current orientation

    0 讨论(0)
  • 2021-02-15 16:06

    I've found what I believe to be the beginnings of the solution. It seems the coordinates you and I are seeing are being based on the bottom left or top right, depending on whether the orientation is UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft.

    I don't know why yet, but hopefully that helps. :)

    [UPDATE] So I guess the origin of the window is 0,0 in normal portrait mode, and rotates with the ipad/iphone.

    So here's how I solved this.

    First I grab my orientation, window bounds and the rect of my view within the window (with the wonky 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 reverse 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 for fixing the origin to be based on the top left no matter the rotation of the ipad/iphone. It fixes the origin depending on where 0,0 currently lives (depending on the orientation)

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

    Here are the two functions 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;
    }
    
    0 讨论(0)
  • 2021-02-15 16:08

    Your code should be:

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

    That will give you the view's rectangle in the window's coordinate system. Be sure to use "bounds" and not "frame". frame is the rectangle of the view in its parent view coordinate system already. "bounds" is the view rectangle in its own system. So the above code asks the table view to convert its own rectangle from its own system to the window's system. Your previous code was asking the table's parent view to convert the table's rectangle from the parent coordinate system to nothing.

    0 讨论(0)
  • 2021-02-15 16:13

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

    0 讨论(0)
提交回复
热议问题