Window has strange frame and offset after rotation on iOS 8

In iOS 8, when I run my app in landscape mode, I get weird values ​​for the key window after rotation:

// Before rotation (landscape)
{{0, 0}, {768, 1024}}

// After rotation (portrait)
{{-256, 0}, {1024, 768}}

      

code:

NSLog(@"%@", NSStringFromCGRect(UIApplication.sharedApplication.keyWindow.frame));

      

Where does the X offset come from and why are the frame values ​​inverted (should be width = 768 in landscape mode)?

+3


source to share


1 answer


I had this problem and fixed it with fixedCoordinateSpace.bounds

. Here's a quick example.

Watch the status bar for changes.

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationDidChanged) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationDidChanged) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
        }
    return self;
}

      



Then rotate the window and resize the window.

- (void)statusBarFrameOrOrientationDidChanged {
    CGRect rect = ({
        CGRect rect;

        if ([[[UIDevice currentDevice] systemVersion] intValue] <= 7) {
            rect = [UIScreen mainScreen].bounds;

        } else {
            id<UICoordinateSpace> fixedCoordinateSpace = [UIScreen mainScreen].fixedCoordinateSpace;
            rect = fixedCoordinateSpace.bounds;
        }

        rect;
    });

    CGAffineTransform transform = CGAffineTransformMakeRotation(({
        CGFloat angle;

        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationPortraitUpsideDown:
                angle = M_PI;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                angle = -M_PI_2;
                break;
            case UIInterfaceOrientationLandscapeRight:
                angle = M_PI_2;
                break;
            default:
                angle = 0.0f;
                break;
        }

        angle;
    }));

    if (!CGAffineTransformEqualToTransform(self.transform, transform)) {
        self.transform = transform;
    }

    if (!CGRectEqualToRect(self.frame, rect)) {
        self.frame = rect;
    }
}

      

+1


source







All Articles