我有一个空应用程序,并且没有涉及到故事板或xib。我想有一个隐藏的状态栏,只支持横向。再次,我不想仅在代码内进行这些更改,也不要触摸Info.plist。使用横向方向时尺寸错误的UIWindow
问题
我创建一个控制器,上面写着唯一支持的方向是横向它是一个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
什么我试过到目前为止
如果我设置RootViewController的后调用makeKeyAndVisible,一切似乎都起作用了。
self.window.backgroundColor = [UIColor cyanColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[AppViewController alloc] init];
还有一些问题。首先我不喜欢这个,因为它看起来很脆弱。第二个问题是,在一个更复杂的应用程序,设置一个GLKViewController作为RootViewController的我得到以下结果(预计将是对左侧没有黑色区域):
看起来状态栏不隐藏得够早。几个手势识别是活动的,在GLKViewController并点击黑色区域产生以下日志消息:
2014年9月25日13:20:42.170 StackOverflowExample [6971:107907]在 意想不到零窗口_UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow:UIClassicWindow:0x7fa20b805e00; frame =(0 0; 375 667); userInteractionEnabled = NO; gestureRecognizers = NSArray: 0x7fa20b80a620;层= UIWindowLayer:0x7fa20b806890
我还进行了各种其他更改,如附加一个空的UIViewController并添加我的视图作为子视图。在这种情况下,我的视图看起来是正确的,但窗口仍然使用错误的尺寸。
如果我没有重写我的视图控制器中supportedInterfaceOrientations方法,一切正常旋转。但那当然不是我想要的。
感谢分享。我花了很多时间试图找出如何做到这一点。 – 2014-10-14 10:01:36
使用iOS8.1和使用Swift,我需要添加一小部分的UIWindow的宽度和高度来解决我的问题,因为在这里完成:http://stackoverflow.com/a/26452679/190599 – CodeReaper 2014-10-29 19:40:38
很好,谢谢你许多。 – 2014-12-15 17:11:24