UINavigationController Force Rotate

后端 未结 3 697
离开以前
离开以前 2020-11-30 06:20

My application is primarily portrait, however there is one view that REQUIRES a landscape orientation.

My views are contained within a UINavigationController, which

3条回答
  •  一向
    一向 (楼主)
    2020-11-30 06:32

    I had the same requirement for one of my applications!!!

    luckily I found a solution!

    In order to keep main viewcontroller landscape, no matter from what orientation it was popped/pushed, I did the following thing: (in viewWillAppear:)

    //set statusbar to the desired rotation position
    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    
    //present/dismiss viewcontroller in order to activate rotating.
    UIViewController *mVC = [[[UIViewController alloc] init] autorelease];
    [self presentModalViewController:mVC animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    

    P.S.Tested on sdk 3.2.5 ios 5.0.1.

    P.S. On iOS 8 previous answer results some screen flickering and also - it is not stable (In some cases It does not work for me anymore.) So, for my needs, I changed the code to: (ARC)

    //set statusbar to the desired rotation position
    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    
    [self.navigationController presentViewController:[UIViewController new] animated:NO completion:^{
        dispatch_after(0, dispatch_get_main_queue(), ^{
            [self.navigationController dismissViewControllerAnimated:NO completion:nil];
        });
    }];
    
    //I added this code in viewDidDissapear on viewController class, which will be popped back.
    

    Hopefully it will help!

提交回复
热议问题