Is there a documented way to set the iPhone orientation?

后端 未结 17 1059
清歌不尽
清歌不尽 2020-11-22 11:09

I have an app where I would like to support device rotation in certain views but other don\'t particularly make sense in Landscape mode, so as I swapping the views out I wou

相关标签:
17条回答
  • 2020-11-22 11:39

    If you want to force it to rotate from portrait to landscape here is the code. Just note that you need adjust the center of your view. I noticed that mine didn't place the view in the right place. Otherwise, it worked perfectly. Thanks for the tip.

    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
    
            [UIView beginAnimations:@"View Flip" context:nil];
            [UIView setAnimationDuration:0.5f];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
            self.view.transform = CGAffineTransformIdentity;
            self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
            self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
            self.view.center = CGPointMake(160.0f, 240.0f);
    
            [UIView commitAnimations];
    }
    
    0 讨论(0)
  • 2020-11-22 11:41

    I found a solution and wrote something in french (but code are in english). here

    The way is to add the controller to the window view (the controller must possess a good implementation of the shouldRotate.... function).

    0 讨论(0)
  • 2020-11-22 11:42

    iOS 6 solution:

    [[[self window] rootViewController] presentViewController:[[UIViewController alloc] init] animated:NO completion:^{
        [[[self window] rootViewController] dismissViewControllerAnimated:NO completion:nil];
    }];
    

    The exact code depends per app and also where you place it (I used it in my AppDelegate). Replace [[self window] rootViewController] with what you use. I was using a UITabBarController.

    0 讨论(0)
  • 2020-11-22 11:42

    I don't think this is possible to do at run-time, though you could of course just apply a 90 degree transform to your UI.

    0 讨论(0)
  • 2020-11-22 11:44

    This is long after the fact, but just in case anybody comes along who isn't using a navigation controller and/or doesn't wish to use undocumented methods:

    UIViewController *c = [[UIViewController alloc]init];
    [self presentModalViewController:c animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    [c release];
    

    It is sufficient to present and dismiss a vanilla view controller.

    Obviously you'll still need to confirm or deny the orientation in your override of shouldAutorotateToInterfaceOrientation. But this will cause shouldAutorotate... to be called again by the system.

    0 讨论(0)
  • 2020-11-22 11:44

    There is a simple way to programmatically force iPhone to the necessary orientation - using two of already provided answers by kdbdallas, Josh :

    //will rotate status bar
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    
    
    //will re-rotate view according to statusbar
    UIViewController *c = [[UIViewController alloc]init];
    [self presentModalViewController:c animated:NO];
    [self dismissModalViewControllerAnimated:NO];
    [c release];
    

    works like a charm :)

    EDIT:

    for iOS 6 I need to add this function: (works on modal viewcontroller)

    - (NSUInteger)supportedInterfaceOrientations
    {
        return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
    }
    
    0 讨论(0)
提交回复
热议问题