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

Posted

技术标签:

【中文标题】如何在控制设备方向的同时控制设备状态栏的方向?【英文标题】:How to control the orientation of the status bar of device while controlling the device orientation? 【发布时间】:2012-03-20 01:12:27 【问题描述】:

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

- (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】:

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

【讨论】:

以上是关于如何在控制设备方向的同时控制设备状态栏的方向?的主要内容,如果未能解决你的问题,请参考以下文章

收到方向更改通知时状态栏方向错误

如何在 Objective-C 中不旋转的视图控制器中检测设备的真实方向?

错误的iOS8状态栏?

方向/配置更改后如何维护 ListView 片段状态?

更改 iPad 方向、旋转状态栏、工具栏,但不更改主视图

无法使用导航控制器锁定视图控制器上的设备方向