Search code examples
iphonexcodeviewautorotateparentviewcontroller

Only having one view autorotate in xcode?


Ok so I currently have 3 views and I need only one of them to autorotate to any orientation while the rest stay in portrait. Right now my set up is a splashviewcontroller fades into view A, and inside view A is a button to switch to view B. All I want is for view B to be able to rotate to any orientation.

When I return YES for shouldautorotatetointerfaceorientation in the splashviewcontroller, every view rotates because this is the parent view. When I return portrait only in the splashview, nothing rotates even if the other views return YES. Is there a way to only have view B rotate? I'm willing to do it manually if you can provide code. Thanks


Solution

  • You can manually mange the rotation of any desired UIView object like so:

    EDIT:

    In the init or viewDidLoad

    - (void)viewDidLoad
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];
    
        [super viewDidLoad];
    }
    
    #define degreesToRadian(x) (M_PI * (x) / 180.0)
    
    -(void)rotate{
    
        self.view.bounds = CGRectMake(0, 0, 0, 0);
    
        if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait){
    
    
            CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
            landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    
            self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);
    
            [self.view setTransform:landscapeTransform];
    
    
        } else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown){
    
    
            CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(180));
            landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    
            self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);
    
            [self.view setTransform:landscapeTransform];
    
        } else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight){
    
            CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
    
             landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
             self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 
    
    
            [self.view setTransform:landscapeTransform];
    
        }else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft){
    
            CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    
            landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
            self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320); 
    
            [self.view setTransform:landscapeTransform];
        }
    
    }