我找到的最佳解決方案是創建一個透明的容器視圖,將該容器添加到窗口,並將您的警報放入容器中。然後您可以註冊UIApplicationWillChangeStatusBarOrientationNotification
以接收輪換事件並轉換容器;這使您可以獨立操作警報的框架:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
self.container = [[UIView alloc] initWithFrame:self.window.bounds];
[self.container addSubview:self.alertView];
[self.window addSubview:self.container];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarWillRotate:)
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
...
}
...
- (void)statusBarWillRotate:(NSNotification *)theNotification
{
CGFloat rotation = 0;
switch ([notification[UIApplicationStatusBarOrientationUserInfoKey] intValue])
{
case UIInterfaceOrientationLandscapeLeft:
rotation = -M_PI_2;
break;
case UIInterfaceOrientationLandscapeRight:
rotation = M_PI_2;
break;
case UIInterfaceOrientationPortraitUpsideDown:
rotation = M_PI;
break;
case UIInterfaceOrientationPortrait:
default:
rotation = 0;
break;
}
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(rotation);
CGSize rotatedSize = CGRectApplyAffineTransform(self.window.bounds, rotationTransform).size;
[UIView animationWithDuration:[UIApplication sharedApplication].statusBarOrientationAnimationDuration
animations:^{
self.container.transform = rotationTransform;
// Transform invalidates the frame, so use bounds/center
self.container.bounds = CGRectMake(0, 0, rotatedSize.width, rotatedSize.height);
self.container.center = CGPointMake(self.window.bounds.size.width/2, self.window.bounds.size.height/2);
}];
}
你可以,如果你真的想,與其windowLevel
屬性設置爲UIWindowLevelAlert
創建一個全新的窗口,這將保證窗口保持在高於所有其他包括鍵盤在內的視圖,但窗口管理變得棘手。上述解決方案應滿足大多數需求。
與簡單地增加一個視圖控制器的視圖到一個窗口的問題是,它不能保證接收所有旋轉和view[Will|Did][Disa|A]ppear:
消息,除非它是通過addChildViewController:
添加到視圖控制器層次根視圖控制器上。
如何將視圖註冊到設備以接收旋轉事件?它不是視圖控制器,所以沒有代理功能。我是否必須趕上UIDeviceOrientationDidChangeNotification以更改爲相應的方向? – 2012-01-08 02:53:18