我有一个iPad应用程序,我UIWindow
在应用程序的开头创建一个新的,并在我进行一些资源同步时显示它.当iPad处于纵向方向时启动应用程序时,一切都很好.然而,当在横向方向上时,新创建的UIWindow's
尺寸似乎很好,但它看起来是侧面的,它的坐标似乎都很奇怪.以下是纵向和横向方向的屏幕截图:
通过向右旋转一次获得景观.
我创建和显示的代码片段UIWindow
如下:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame];
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
self.progressWindow.windowLevel = UIWindowLevelAlert;
/* some other code to create the subviews */
[self.progressWindow makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
此问题仅发生在iOS 8上.
据我了解,当方向改变时,新创建的 UIWindow 不会自动旋转。我不知道这是否是 iOS 8 的新功能还是一个错误,但我能够使用以下代码克服该问题:
UIWindow *window=[UIApplication sharedApplication].keyWindow;
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame];
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
self.progressWindow.windowLevel = UIWindowLevelAlert;
/* some other code to create the subviews */
[self handleOrientationChange];
[self.progressWindow makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientationChange) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
handleOrientationChange 的实现如下:
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
- (void)handleOrientationChange
{
if (self.progressWindow)
{
// rotate the UIWindow manually
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
[self.progressWindow setTransform:[self transformForOrientation:orientation]];
// resize the UIWindow according to the new screen size
if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)])
{
// iOS 8
CGRect screenRect = [UIScreen mainScreen].nativeBounds;
CGRect progressWindowFrame = self.progressWindow.frame;
progressWindowFrame.origin.x = 0;
progressWindowFrame.origin.y = 0;
progressWindowFrame.size.width = screenRect.size.width / [UIScreen mainScreen].nativeScale;
progressWindowFrame.size.height = screenRect.size.height / [UIScreen mainScreen].nativeScale;
self.progressWindow.frame = progressWindowFrame;
}
else
{
// iOs 7 or below
CGRect screenRect = [UIScreen mainScreen].bounds;
CGRect progressWindowFrame = self.progressWindow.frame;
progressWindowFrame.origin.x = 0;
progressWindowFrame.origin.y = 0;
progressWindowFrame.size.width = screenRect.size.width;
progressWindowFrame.size.height = screenRect.size.height;
self.progressWindow.frame = progressWindowFrame;
}
}
}
- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
return CGAffineTransformMakeRotation(-DegreesToRadians(90));
case UIInterfaceOrientationLandscapeRight:
return CGAffineTransformMakeRotation(DegreesToRadians(90));
case UIInterfaceOrientationPortraitUpsideDown:
return CGAffineTransformMakeRotation(DegreesToRadians(180));
case UIInterfaceOrientationPortrait:
default:
return CGAffineTransformMakeRotation(DegreesToRadians(0));
}
}
Run Code Online (Sandbox Code Playgroud)
我从这个问题的公认答案中得到了这个想法。
归档时间: |
|
查看次数: |
363 次 |
最近记录: |