Supporting both iOS 6 and iOS 5 autorotation

2019-01-14 23:06发布

Can anyone confirm that to support both iOS 6 and iOS 5 there is no point to adding the new iOS 6 autorotation methods, since the Apple docs suggest that these methods are completely ignored if you are also implementing the iOS 5 methods?

In particular, I talking about the methods - (NSUInteger)supportedInterfaceOrientations and - (BOOL) shouldAutorotate -- that these are ignored and synthesized by the compiler if you also implement the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

6条回答
霸刀☆藐视天下
2楼-- · 2019-01-14 23:26

For iOS5 Rotation. Check for your desired orientation and return YES

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{  
    if ((interfaceOrientation==UIInterfaceOrientationPortrait)||(interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)) {
        return YES;
    }
    else return NO;
}

iOS 6.0 Autorotation Support

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationMaskPortrait|| UIInterfaceOrientationMaskPortraitUpsideDown);

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationIsPortrait(UIInterfaceOrientationPortrait|| UIInterfaceOrientationPortraitUpsideDown);

}
查看更多
贪生不怕死
3楼-- · 2019-01-14 23:28

You need to add new callback for autorotation if you are packaging your app in the new sdk. However, these callbacks will be received only when such an app is run on iOS 6 devices. For devices running on earlier iOS versions, earlier callbacks would be received. If you dont implement the new callbacks, the default behavior is that your app runs on all orientation on iPad and all except UpsideDown orientation on iPhone.

查看更多
The star\"
4楼-- · 2019-01-14 23:29

I think the most elegant solution is:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return ((1 << toInterfaceOrientation) & self.supportedInterfaceOrientations) != 0;
}

Do I miss something?

查看更多
时光不老,我们不散
5楼-- · 2019-01-14 23:33

You have to set a flag somewhere (I believe in your Info.plist) that indicates which of the two you use. If you use the new one, you can't build for iOS 5 (or at least, it won't work on iOS 5). If you use the old one, the new methods aren't being called. So yeah, you pretty much have to choose which method(s) you want to use, and if you want to support iOS 5, you cannot use the new methods.

查看更多
Juvenile、少年°
6楼-- · 2019-01-14 23:43

to support autorotations in both ios5 and ios6 we need to provide call backs in case of ios6....

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification object:nil];

and we need to call

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;

} 

-(BOOL)shouldAutoRotate{
    return YES;
  }

for ios5

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return ((toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
查看更多
看我几分像从前
7楼-- · 2019-01-14 23:49
 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskAll;
}

-(void)viewWillLayoutSubviews
{
if([self interfaceOrientation] == UIInterfaceOrientationPortrait||[self interfaceOrientation] ==UIInterfaceOrientationPortraitUpsideDown)
    {
       if (screenBounds.size.height == 568)
        {
          //set the frames for 4"(IOS6) screen here
         }
        else

        {
         ////set the frames for 3.5"(IOS5/IOS6) screen here
        }

}
        else if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft||[self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
        {
         if (screenBounds.size.height == 568)
        {
          //set the frames for 4"(IOS6) screen here
         }
        else

        {
         ////set the frames for 3.5"(IOS5/IOS6) screen here
        }


    }
//it is for IOS5
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
     return YES;
    }

-(void)viewWillLayoutSubviews this method will call for ios5/ios6. this code is usefull to both ios6/ios5/ios6 with 3.5" screen/4" screen with ios6.

查看更多
登录 后发表回答