【问题标题】:Status bar rotates separated from UIWindow状态栏与 UIWindow 分开旋转
【发布时间】:2012-10-23 11:54:43
【问题描述】:

我必须用两个UIWindow 创建应用程序(请不要问为什么)。首先UIWindowrootViewController 支持所有方向。第二个 - 只有纵向和倒置。所以应用程序委托代码如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // part one
    self.windowOne = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.windowOne.rootViewController = [[ViewControllerOne alloc] init]; // supports all orientations
    self.windowOne.rootViewController.view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]] autorelease];

    [self.windowOne makeKeyAndVisible];
    //~part one
    // part two
    self.windowTwo = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.windowTwo.rootViewController = [[ViewControllerTwo alloc] init]; // supports only portrait and upside down
    self.windowTwo.rootViewController.view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2.png"]] autorelease];

    [self.windowTwo makeKeyAndVisible];
    //~part two
    return YES;
}

如果两个部分中只有一个处于活动状态(第二个被评论)- 一切正常。但是如果 windowOne 在 windowTwo 之前已经成为 key 并且可见,windowTwo 会在 ViewControllerTwo 允许的情况下旋转,但状态栏的行为非常奇怪:它像 windowOne 一样旋转是 key 并且可见。

是否有任何选项可以让状态栏像ViewControllerTwo 所说的那样旋转?

【问题讨论】:

    标签: objective-c ios uiviewcontroller uiwindow


    【解决方案1】:

    虽然这对我来说是个 hack,但您可以尝试如下:

    在您的视图控制器shouldAutorotateToInterfaceOrientation(适用于iOS5)或shouldAutorotate(适用于iOS6)方法中添加以下代码行:

    [[UIApplication sharedApplication] setStatusBarOrientation: interfaceOrientation animated: YES];
    

    然后测试应用程序的行为。

    要在(iOS6 的新功能)shouldAutorotate 方法中获取界面方向,请执行以下操作:

    UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
    

    我没有亲自测试这一切,但它可以工作。

    (查看post 了解如何在 iOS6 下支持自动旋转)

    【讨论】:

    • 感谢您的回复。已尝试,但结果行为不稳定。有时,状态栏会出现在侧面,而不仅仅是顶部或底部。
    • 嗯,您还可以尝试将 setStatusBarOrientation 代码也放入您的控制器 viewWillAppear/viewDidAppear 方法中。但最终这一切对我来说并不是最佳实践。
    • 问题不在出现/消失时出现,而是在视图已经可见时快速设备旋转时出现。似乎描述的情况是无证行为。
    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多