UIView 不处理旋转, UIViewController 处理。因此,您只需要创建一个 UIViewController,它实现 shouldAutorotateToInterfaceOrientation 并将此控制器设置为您的 UIWindow 的 rootViewController
像这样的东西:
UIViewController * vc = [[[MyViewController alloc] init] autorelease];
vc.view.frame = [[UIScreen mainScreen] bounds];
vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
//you vc.view initialization here
UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.windowLevel = UIWindowLevelStatusBar;
window.backgroundColor = [UIColor clearColor];
[window setRootViewController:vc];
[window makeKeyAndVisible];
我使用了这个 MyViewController,因为我希望它反映主应用程序的变化
@interface MyViewController : UIViewController
@end
@implementation MyViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
UIWindow *window = ((UIWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]);
UIViewController *controller = window.rootViewController;
if (!controller) {
NSLog(@"%@", @"I would like to get rootViewController of main window");
return YES;
}
return [controller shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
@end
但是,如果您愿意,您始终可以对任何方向返回 YES 或编写您的逻辑。