关于Objective C:如何在控制设备方向的同时控制设备状态栏的方向?

在我的应用中,有一项要求,即每当用户将设备旋转到横向模式时,它都应显示特定的横向视图,并应显示向后旋转时显示的最后一个视图。
我可以使用它,但是现在的问题是,当用户旋转设备使其横向显示风景时,其状态栏也会旋转,但是当将其旋转回纵向时,状态栏不会变为纵向,而是保持为横向模式,并且视图显示为纵向。
这是我正在使用的代码段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
- (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;
}

请帮忙!!!!!

谢谢,


您可以使用UIApplication

中的-setStatusBarOrientation:animated:方法