ios - 窗口在 iOS 8 上旋转后有奇怪的框架和偏移

标签 ios rotation ios8 frame

在 iOS 8 上,当以横向模式启动应用程序时,我在旋转后得到关键窗口框架的奇怪值:

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

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

代码:

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

X 偏移从何而来,为什么帧值会倒置(横向模式下应该是 width=768)?

最佳答案

我遇到了这个问题并使用 fixedCoordinateSpace.bounds 修复了它。这是一个简单的例子。

观察窗口中状态栏的变化。

- (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;
}

然后旋转窗口并调整窗口框架的大小。

- (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;
    }
}

关于ios - 窗口在 iOS 8 上旋转后有奇怪的框架和偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26103522/

相关文章:

ios - 将 [NSNull null] 值存储在 NSUserDefaults 中,来自 JSON 序列化,会导致不需要的异常

ios - 将 iOS 7 常量映射到 2G、3G、4G、LTE 等

ios - 如何将滑动删除添加到堆栈 View

ios - 与应用程序异步更新 map 上的注释

html - 部分旋转后如何 "unrotate"一个div?

c - 点奇怪地旋转,我的计算有什么问题?

c# - 旋转后更改 WPF 元素的父元素(设置新坐标问题)

iOS 8 : Remove sensitive information from views before moving to the background

ios - 在 iOS 8/9 中根据设备方向更改 Viewcontroller

ios - 错误的 iOS8 状态栏?