How to control the orientation of the status bar of device while controlling the device orientation?

StackOverflow https://stackoverflow.com/questions/9780184

質問

In my app there is a requirement that it should display a specific landscape view whenever user rotate the device to landscape mode and should show the last view displayed when rotate back. I got it working but now the problem is when user rotate the device to landscape its status bar also rotate but while rotating it back to portrait the status bar is not going portrait but staying in Landscape mode with the view displayed in Portrait. Here's the code snippets i am using:

- (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;
}

Please help!!!!!

Thanks,

役に立ちましたか?

解決

You can use the -setStatusBarOrientation:animated: method from UIApplication

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top