在iOS 8上旋转后,窗口具有奇怪的帧和偏移
在iOS 8上,当以横向模式启动应用程序时,我会在旋转后得到关键窗口帧的奇怪值:在iOS 8上旋转后,窗口具有奇怪的帧和偏移,ios,rotation,ios8,frame,Ios,Rotation,Ios8,Frame,在iOS 8上,当以横向模式启动应用程序时,我会在旋转后得到关键窗口帧的奇怪值: // Before rotation (landscape) {{0, 0}, {768, 1024}} // After rotation (portrait) {{-256, 0}, {1024, 768}} 代码: X偏移从何而来,为什么帧值会反转(在横向模式下宽度应为768)?我遇到了这个问题,并使用fixedCoordinateSpace.bounds修复了它。这里有一个简单的例子 观察窗口中状态栏的
// Before rotation (landscape)
{{0, 0}, {768, 1024}}
// After rotation (portrait)
{{-256, 0}, {1024, 768}}
代码:
X偏移从何而来,为什么帧值会反转(在横向模式下宽度应为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;
}
}
-(无效)状态栏框架或方向ID已更改{
CGRect rect=({
CGRect rect;
如果([[[UIDevice currentDevice]systemVersion]intValue]我看到了同样的问题-这真是令人沮丧
- (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;
}
}