如何在控制设备方向的同时控制设备状态栏的方向?

时间:2012-03-20 01:12:28

标签: objective-c ios xcode uideviceorientation

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

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

请帮忙!!!!!

谢谢,

1 个答案:

答案 0 :(得分:2)

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