在 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/