Forcing landscape and autorotate in iOS 7

2019-02-12 08:44发布

My app is supposed to be landscape only, and I had no problem with this when building for iOS 6 and earlier. Now with iOS 7, It won't rotate at all.

In my app settings, I have it set to landscape left/right only. In my view controller, i'm using the following:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

I also used to use this, which is now deprecated:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
    return UIInterfaceOrientationIsLandscape(orientation);
}

The new one appears to be shouldAutorotate, but using this crashes my app. Any ideas on this would be appreciated, since my app is forced to portrait on my iPad and in the simulator. Thank you!

4条回答
啃猪蹄的小仙女
2楼-- · 2019-02-12 09:23

This solves my problem. I'm not sure why I had issues before, but I must have missed trying this exact combination (also, info.plist should have the supported orientations set).

(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

(BOOL)shouldAutorotate {
    return YES;
}

edit: I may have having issues with the simulator, and doing a reset/restart and clean might have contributed to the fix.

查看更多
等我变得足够好
3楼-- · 2019-02-12 09:24

I may have having issues with the simulator, and doing a reset/restart and clean might have contributed to the fix.

This worked for me: (Simulator -> Reset Content and Settings...)

查看更多
你好瞎i
4楼-- · 2019-02-12 09:26

Include this method as well in your code:

- (BOOL)shouldAutorotate{
  if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft ||[[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight)
  {
    return YES;
  }
  else{
    return NO;
  }
}

Read this for more info. Here it is mentioned that we should override shouldAutorotate to suppress orientations.

If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate method on the topmost view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-12 09:32

i don't know why, but this work for me on IOS 7

[[UIApplication sharedApplication] setStatusBarHidden:NO];

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

[super willRotateToInterfaceOrientation:UIInterfaceOrientationPortrait duration:0];
查看更多
登录 后发表回答