【发布时间】:2014-11-20 03:51:55
【问题描述】:
我有一个空的应用程序,没有涉及故事板或 xib。我想要一个隐藏的状态栏并且只支持横向。同样,我不想只在代码中进行这些更改,也不要触摸 Info.plist。
问题
我创建了一个带有控制器的 UIWindow,它说唯一支持的方向是横向。在这种情况下,我的 UIWindow 是在纵向模式的维度中创建的,并且不会旋转。预期的结果将是一个完全青色的屏幕。
这是我的代表:
#import "AppDelegate.h"
#import "AppViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor cyanColor];
self.window.rootViewController = [[AppViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
@end
这是我的控制器:
#import "AppViewController.h"
@implementation AppViewController
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
@end
到目前为止我已经尝试过什么
如果我在调用 makeKeyAndVisible 之后设置 rootViewController,一开始一切似乎都正常。
self.window.backgroundColor = [UIColor cyanColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[AppViewController alloc] init];
还有一些问题。首先我不喜欢这个,因为它看起来很脆弱。第二个问题是,在将 GLKViewController 设置为 rootViewController 的更复杂的应用程序中,我得到以下结果(预计左侧没有黑色区域):
看起来状态栏隐藏得不够早。几个手势识别器处于活动状态,在 GLKViewController 中单击黑色区域会产生以下日志消息:
2014-09-25 13:20:42.170 StackOverflowExample[6971:107907] 出现意外的 nil 窗口 _UIApplicationHandleEventFromQueueEvent,_windowServerHitTestWindow:UIClassicWindow:0x7fa20b805e00;帧 = (0 0; 375 667); 用户交互启用 = 否;手势识别器 = NSArray: 0x7fa20b80a620;层 = UIWindowLayer: 0x7fa20b806890
我还执行了各种其他更改,例如附加一个空的 UIViewController 并将我的视图添加为子视图。在这种情况下,我的视图看起来正确,但窗口仍然使用错误的尺寸。
如果我不覆盖视图控制器中的 supportedInterfaceOrientations 方法,一切都会正确旋转。但这当然不是我想要的。
【问题讨论】:
标签: ios objective-c iphone landscape uiwindow