7

keyWindow ではない にを追加しUIViewています。デバイスが回転するときに を回転させUIWindowたいです。設定する必要があるウィンドウまたはビューの特別なプロパティはありますか? 現在、ビューは回転していません。UIView

keyWindowアプリケーションの最初のサブビューのみがデバイスの回転について通知されるという事実を認識しています。テストとして、ビューを の最初のサブビューに追加しましたkeyWindow。これにより、ビューが回転します。ただし、keyWindowの最初のサブビューのサブビューであるビューは、さまざまな審美的な理由で機能しません。

別のアプローチは、ビューでデバイスの向きの変化を観察し、回転コードを自分で書くことです。ただし、可能であれば、このコードを作成することは避けたいと思います (私の意見では、ウィンドウを追加する方がクリーンです)。

4

2 に答える 2

7

UIView は回転を処理しませんが、UIViewController は処理します。したがって、必要なのは UIViewController を作成することだけです。これは shouldAutorotateToInterfaceOrientation を実装し、このコントローラーを rootViewController として UIWindow に設定します。

そんな感じ:

    UIViewController * vc = [[[MyViewController alloc] init] autorelease];

    vc.view.frame = [[UIScreen mainScreen] bounds];
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

    //you vc.view initialization here

    UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.windowLevel = UIWindowLevelStatusBar;
    window.backgroundColor = [UIColor clearColor];
    [window setRootViewController:vc];
    [window makeKeyAndVisible];

そして、このMyViewControllerを使用しました。メインアプリケーションの変更を反映させたいからです

    @interface MyViewController : UIViewController

    @end

    @implementation MyViewController

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        UIWindow *window = ((UIWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]);
        UIViewController *controller = window.rootViewController;

        if (!controller) {
            NSLog(@"%@", @"I would like to get rootViewController of main window");
            return YES;
        }
        return [controller shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    @end

ただし、必要に応じて、いつでも YES を返すか、ロジックを記述することができます。

于 2012-06-17T00:06:31.600 に答える
2

以下のコードをプロジェクトに追加できます

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

そして、それを処理する関数を作成しました。

于 2012-01-05T04:30:26.630 に答える