shouldAutorotateToInterfaceOrientation is not working in iOS 6

*爱你&永不变心* 提交于 2019-11-26 03:10:55

问题


In iOS 6 shouldAutorotateToInterfaceOrientation is not working but it work fine in iOS 5.0 or 5.1.

What should i need to change in iOS 6. Here is my code

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if([[[SampleApplicationAppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE])
{
    int nAngle = 0;
    BOOL bRet = NO;

    switch (interfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            nAngle = 90;
            bRet = YES;
            NSLog(@\".......Preview = %f %f\",_previewCamera.frame.size.width,_previewCamera.frame.size.height);

            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);

            NSLog(@\"Preview = %f %f\",_previewCamera.frame.size.width,_previewCamera.frame.size.height);
            break;

        case UIInterfaceOrientationPortraitUpsideDown:
            nAngle = 270;
            bRet = YES;
            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
            break;

        case UIInterfaceOrientationLandscapeLeft:
            nAngle = 0;
            bRet = YES;
            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);
            break;

        case UIInterfaceOrientationLandscapeRight:
            nAngle = 180;
            bRet = YES;
            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
            break;

        default:
            break;
    }                
    return bRet;
}    
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    return YES;     
return NO;    

}

when i search for this orientation problem i found all this

1 2

but nothing work for me :( Please help .....


回答1:


EDIT: This is happening because Apple has changed the way of managing the Orientation of UIViewController. In iOS6 orientation handles differently. In iOS6 shouldAutorotateToInterfaceOrientation method is deprecated. View controllers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.

If you want a specific view to be changed to the desired orientation you have to do some sort of subclass or category and override the autorotation methods to return the desired orientation.

Place this code in your root view controller. This will help the UIViewController to determine its orientation.

  //RotationIn_IOS6 is a Category for overriding the default orientation.

  @implementation UINavigationController (RotationIn_IOS6)

 -(BOOL)shouldAutorotate
    {
      return [[self.viewControllers lastObject] shouldAutorotate];
    }

  -(NSUInteger)supportedInterfaceOrientations
   {
     return [[self.viewControllers lastObject] supportedInterfaceOrientations];
   }

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
     return [[self.viewControllers lastObject]  preferredInterfaceOrientationForPresentation];
 }

 @end

Now you need to implement below methods (introduced in iOS6) in viewController for orientation

- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return TRUE;


}
- (NSUInteger)supportedInterfaceOrientations
{   
     //decide number of origination tob supported by Viewcontroller.
     return UIInterfaceOrientationMaskAll;


}

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
   {
     //from here you Should try to Preferred orientation for ViewController 
   }

And put your code inside the below method. Whenever device orientation is changed this method will be called:

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
{
if([[[SampleApplicationAppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE])
{
    int nAngle = 0;
    BOOL bRet = NO;

    switch (interfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            nAngle = 90;
            bRet = YES;
            NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);

            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);

            NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);
            break;

        case UIInterfaceOrientationPortraitUpsideDown:
            nAngle = 270;
            bRet = YES;
            _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
            break;

        case UIInterfaceOrientationLandscapeLeft:
            nAngle = 0;
            bRet = YES;
            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);
            break;

        case UIInterfaceOrientationLandscapeRight:
            nAngle = 180;
            bRet = YES;
            //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
            break;

        default:
            break;
    }                
  }    

Edit: check your window, you need to add the controller on window as rootViewController rather than addSubview like below

self.window.rootViewController=viewController;

For more information here's an article about iOS6.0 Beta 2 OTA.

I hope this was helpful.




回答2:


The way I fixed this problem was to replace the following line when my app starts in Delegate Class

 window addSubview: navigationController.view

with

window.rootViewController = navigationController

After I made this change my app started to handle screen rotations




回答3:


This is because Apple has deprecated shouldautorate method from ios6 use these methods instead

- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);



回答4:


I Solved this problem just use this

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    
    if(![AppDelegate instance])
        return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);

    if([[[AppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE])
    {
        int nAngle = 0;
        BOOL bRet = NO;

        switch (interfaceOrientation) {
            case UIInterfaceOrientationPortrait:
            {
                nAngle = 90;
                bRet = YES;
                NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);

                NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];
                if (order == NSOrderedSame || order == NSOrderedDescending)
                {
                    // OS version >= 6.0
                    NSLog(@"ami iOS 6 er device");
                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
                }
                else
                {
                    // OS version < 6.0
                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);
                }

                NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height);
                NSLog(@"-->%s %d",__FUNCTION__,__LINE__);
                break;
            }

            case UIInterfaceOrientationPortraitUpsideDown:
            {

                nAngle = 270;
                bRet = YES;
                NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];
                if (order == NSOrderedSame || order == NSOrderedDescending)
                {
                    // OS version >= 6.0
                    NSLog(@"ami iOS 6 er device");
                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);
                }
                else
                {
                    // OS version < 6.0
                    _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
                }
                break;
            }
            case UIInterfaceOrientationLandscapeLeft:
                nAngle = 0;
                bRet = YES;
                //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5);
                break;

            case UIInterfaceOrientationLandscapeRight:
                nAngle = 180;
                bRet = YES;
                //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2);
                break;

            default:
                break;
        }        

        return bRet;
    }   
    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return YES; 
    return NO;    
}



回答5:


please try this one it will be helpful for you.

write code in you viewcontroller class

-(BOOL)shouldAutorotate
   {
      return YES;
   }

   -(NSUInteger)supportedInterfaceOrientations{
      return UIInterfaceOrientationMaskLandscape;
    }

Then in appdelegate search this line [window addSubview:viewController.view] and replace this with window.rootViewController = viewController;

cheers it will work.its simple solution for ios 6




回答6:


this worked for me

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


    - (BOOL)shouldAutorotate {

        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

        if (orientation == UIInterfaceOrientationPortrait) {
            // your code for portrait mode

        }

        return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown;
    }


来源:https://stackoverflow.com/questions/12577879/shouldautorotatetointerfaceorientation-is-not-working-in-ios-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!