第二个UIWindow中的方向更改

时间:2014-02-17 18:00:18

标签: ios objective-c orientation uiwindow

由于一个阻止我在主UIWindow中显示内容的问题,我已向我的应用添加了第二个UIWindow。我明白不推荐这样做,但我有点被迫沿着这条路走。

无论如何,我已经创建了第二个UIWindow,但我现在正尝试将其设置为响应方向更改,因为它不会自动执行此操作,这与主窗口不同。问题是我对iOS特定编码几乎没有经验,而且我对此有点不知所措。

这是我到目前为止所得到的:

// Create the new UIWindow
mWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, size.height, size.width)];
mWindow.windowLevel = UIWindowLevelStatusBar + 1.0f;
[mP3NMoreGamesWindow setRootViewController:mMyViewController];

MyViewController方向特定功能:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations.
    NSString * supported_orientation = [Utilities GetDeviceOrientation];
    if ([supported_orientation isEqualToString:@"landscape"] && (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight))
    {
        return YES;
    }
    else if ([supported_orientation isEqualToString:@"portrait"] && (interfaceOrientation == UIInterfaceOrientationPortrait ||
                                                                     interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
    {
        return YES;
    }

    return NO;
}

- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSString * supported_orientation = [Utilities GetDeviceOrientation];
    if ([supported_orientation isEqualToString:@"landscape"])
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}

我有什么遗失的吗?我的印象是窗口rootviewcontroller收到了方向更改通知?

此外,是否可以在创建时设置UIWindow的方向?例如,如果在创建UIWindow时当前方向是横向的,我可以将UIWindowfi设置为横向而不是默认情况下的纵向吗?

感谢。

2 个答案:

答案 0 :(得分:1)

解决方案在app委托中。您可以分别处理每个窗口支持的方向:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
   // check, which window is asking and return the mask
   return UIInterfaceOrientationMaskAllButUpsideDown;
}

答案 1 :(得分:0)

好的,我不确定你到底想要完成什么,所以我可能会有点偏离基础。但是,请查看您的委托方法:

    - (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations.
    NSString * supported_orientation = [Utilities GetDeviceOrientation];
    if ([supported_orientation isEqualToString:@"landscape"] && (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight))
    {
        return YES;
    }
    else if ([supported_orientation isEqualToString:@"portrait"] && (interfaceOrientation == UIInterfaceOrientationPortrait ||
                                                                     interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
    {
        return YES;
    }

    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSString * supported_orientation = [Utilities GetDeviceOrientation];
    if ([supported_orientation isEqualToString:@"landscape"])
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}

确保使用某些断点实际调用它们。我猜他们不是。

看看这个:http://shaune.com.au/using-multiple-uiwindows-in-ios-applications/

考虑到所有这些,最好的方法是使用NSNotificationCenter设置通知,以便在第一个窗口旋转时通知您的第二个窗口并手动处理旋转。