2014-09-25 66 views
17

我有一个空应用程序,并且没有涉及到故事板或xib。我想有一个隐藏的状态栏,只支持横向。再次,我不想仅在代码内进行这些更改,也不要触摸Info.plist。使用横向方向时尺寸错误的UIWindow

问题

我创建一个控制器,上面写着唯一支持的方向是横向它是一个UIWindow。在这种情况下,我的UIWindow是在纵向模式的维度中创建的,并且不会旋转。预期的结果将是一个完全青色的屏幕。

Broken 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的我得到以下结果(预计将是对左侧没有黑色区域):

Broken GLKViewController

看起来状态栏不隐藏得够早。几个手势识别是活动的,在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方法,一切正常旋转。但那当然不是我想要的。

回答

24

当您运行景观肖像模式UIScreen有肖像在iOS 8的范围(只有当你有没有这个程序,在应用开关面板,通过iOS 8做了一些缓存)的应用程序。即使显示makeKeyAndVisible窗口也不会改变它的框架。但它根据AppViewController的可用方向更改[UIScreen mainScreen].bounds

#import "AppDelegate.h" 
#import "AppViewController.h" 

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Portrait bounds at this point 
    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 

因此,让我们改变窗口的框架[self.window makeKeyAndVisible]

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [UIWindow new]; 
    self.window.backgroundColor = [UIColor cyanColor]; 
    self.window.rootViewController = [[AppViewController alloc] init]; 
    [self.window makeKeyAndVisible]; 

    // Here it is 
    self.window.frame = [UIScreen mainScreen].bounds; 
    return YES; 
} 

我认为这是iOS 8的错误之后。

+1

感谢分享。我花了很多时间试图找出如何做到这一点。 – 2014-10-14 10:01:36

+0

使用iOS8.1和使用Swift,我需要添加一小部分的UIWindow的宽度和高度来解决我的问题,因为在这里完成:http://stackoverflow.com/a/26452679/190599 – CodeReaper 2014-10-29 19:40:38

+0

很好,谢谢你许多。 – 2014-12-15 17:11:24

0

添加启动画面,您只能通过增加额外的属性到info.plist中

有这个问题,我自己做的时候,问题就解决了,我不知道,如果你可以通过代码添加它虽然,我只设法使其与info.plist中+启动屏幕XIB文件工作

<key>UILaunchStoryboardName</key> 
<string>Launch Screen</string> 

其实我不认为你必须添加一个XIB文件,如果只是关键字(任意值)可在plist它应该工作。

+0

对于我来说,不幸的是没有这样做。 – 2014-09-25 12:08:19

8

我有一个类似的问题,只适用于肖像的应用程序。

我通过设置状态栏方向固定问题之前实例化UIWindow

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Init my stuff 
    // ... 

    // Prepare window 
    [application setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; // prevent start orientation bug 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

在你的情况,你应该在setStatusBarOrientation:animated:方法使用UIInterfaceOrienationLandscapeLeft(或右)。

希望它可以帮助你。

+0

辉煌!没有其他解决方案的工作 – RawMean 2014-11-21 07:34:05

0

这里或其他地方发布的解决方案都不适合我。

但是,我发现这个问题显然不会出现在故事​​板中,所以另一种解决方案是从xibs移开。 (这个事实令人遗憾的是,苹果公司也不会认真对待这个问题。)