简体   繁体   中英

How do I rotate the iOS simulator using code?

Does anyone know how to rotate the iOS Simulator (6.0 and above)? I've searched but have found nothing on this.

I am trying to do this through code (not manually), but if it can be done through a script, then it has to be run from code. Can this even be done? Some advice needed.

Add an entry for Supported interface orientations to the Tests target (or whatever target is appropriate) like this:

目标设置

As long as the only entries are Landscape ones, it will run in Landscape mode.

Edit:
If I misunderstood your comment about tests and you aren't running unit tests, you probably don't want to keep changing the orientation of your main target. In that case, you could duplicate your main target and make the orientation change there.

Edit:
There is an undocumented method to force rotation in code. While this will work for tests, it could get your app rejected if you use it in a submitted app.

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

Just include this category wherever you need to use this method.

@interface UIDevice (MethodsThatAppleWillHitMeWithTheBanStickForUsing)
    -(void)setOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
    -(void)setOrientation:(UIInterfaceOrientation)orientation;
@end

Well there are couple of steps you have to check to make all orientations work in iOS6. Even if one of them is missing it won't work.

  1. As Lance says, yes you have to set the supported orientations for iphone or ipad whatever appropriate.

  2. Write this code in the controller which you want to set the rotation for.

    -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; }

    -(BOOL)shouldAutorotate { return YES; }

Change UIInterfaceOrientationMaskAll depending on what orientations you want.

  1. If you are using a navigation controller in your project make sure you set it properly!

You shouldn't be getting this error...(Applications are expected to have a root view controller at the end of application launch)

Get these steps right and rotation will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM