How do I rotate the iOS simulator using code?

前端 未结 2 1864
生来不讨喜
生来不讨喜 2021-01-05 22:18

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),

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 22:38

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

    Target Settings

    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
    

提交回复
热议问题