1

在我的应用程序中,每当用户将设备旋转到横向模式时,它都应显示特定的横向视图,并且应在向后旋转时显示最后显示的视图。我得到了它的工作,但现在的问题是当用户将设备旋转到横向时,它的状态栏也会旋转,但是在将其旋转回纵向时,状态栏不会变成纵向,而是保持横向模式,视图以纵向显示。这是我正在使用的代码片段:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3, *viewController4, *viewController5;

    // Create initialized instance of UITabBarController
    tabController = [[UITabBarController alloc] init];
    NSMutableArray *tabs = [[NSMutableArray alloc] init];
    // Create first UIViewController
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];
    [viewController1 setTitle:@"Home"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
    [tabs addObject:navController];

    viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil];
    [viewController2 setTitle:@"News"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController2];
    [tabs addObject:navController];

    viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [viewController3 setTitle:@"Music"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController3];
    [tabs addObject:navController];

    viewController4 = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
    [viewController4 setTitle:@"Gallery"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController4];
    [tabs addObject:navController];

    viewController5 = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];
    [viewController5 setTitle:@"Shows"];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController5];
    [tabs addObject:navController];

    landscapeVC = [[LandscapeAboutViewController alloc] initWithNibName:@"LandscapeAboutViewController" bundle:nil];
    [tabController setViewControllers:tabs];
    self.window.rootViewController = tabController;
    [self.window makeKeyAndVisible];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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


    return YES;
}

- (void) didRotate:(NSNotification *)notification
{   
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (UIDeviceOrientationIsLandscape(orientation)) {
        if (self.window.rootViewController != landscapeVC) {
            [self switchToLandScape];
        }

    }
    else if (UIDeviceOrientationIsPortrait(orientation)){
        if (self.window.rootViewController != tabController) {
            [self switchToTabBar];
        }
    }
}

- (void)switchToTabBar {
    self.window.rootViewController = tabController;
}

- (void)switchToLandScape {
    self.window.rootViewController = landscapeVC;
}

请帮忙!!!!!

谢谢,

4

1 回答 1

2

您可以使用 UIApplication 中的 -setStatusBarOrientation:animated: 方法

于 2012-03-20T02:43:57.450 回答