How to force a UIViewController to Portrait orientation in iOS 6

后端 未结 16 1623
别那么骄傲
别那么骄傲 2020-11-22 06:03

As the ShouldAutorotateToInterfaceOrientation is deprecated in iOS 6 and I used that to force a particular view to portrait only, what is the c

16条回答
  •  面向向阳花
    2020-11-22 07:01

    This answer relates to the questions asked in the comments of the OP's post:

    To force a view to appear in a given oriention put the following in viewWillAppear:

    UIApplication* application = [UIApplication sharedApplication];
    if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
    {
        UIViewController *c = [[UIViewController alloc]init];
        [self presentModalViewController:c animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
    

    It's a bit of a hack, but this forces the UIViewController to be presented in portrait even if the previous controller was landscape

    UPDATE for iOS7

    The methods above are now deprecated, so for iOS 7 use the following:

    UIApplication* application = [UIApplication sharedApplication];
    if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
    {
         UIViewController *c = [[UIViewController alloc]init];
         [c.view setBackgroundColor:[UIColor redColor]];
         [self.navigationController presentViewController:c animated:NO completion:^{
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                }];
         }];
    }
    

    Interestingly, at the time of writing, either the present or dismiss must be animated. If neither are, then you will get a white screen. No idea why this makes it work, but it does! The visual effect is different depending on which is animated.

提交回复
热议问题