Search code examples
iphoneiosobjective-cios6uideviceorientation

[self.view addSubview:_topViewController.view]; autorotation issue


Really been stuck for this for weeks. I am using ECSlidingViewController, and i want one view, to be able to rotate to landscape and portrait, as it is a landscape photo, and needs to make use of available space, whilst i don't want the rest of the app to rotate, just stay in landscape.

I'm sure the autorotation methods are not getting called as it uses this technique to switch to views...

- (void)setTopViewController:(UIViewController *)theTopViewController
{
  CGRect topViewFrame = _topViewController ? _topViewController.view.frame : self.view.bounds;

  [self removeTopViewSnapshot];
  [_topViewController.view removeFromSuperview];
  [_topViewController willMoveToParentViewController:_topViewController];
  [_topViewController removeFromParentViewController];

  _topViewController = theTopViewController;

  [self addChildViewController:self.topViewController];
  [self.topViewController didMoveToParentViewController:self];

  [_topViewController.view setAutoresizingMask:self.autoResizeToFillScreen];
  [_topViewController.view setFrame:topViewFrame];
  _topViewController.view.layer.shadowOffset = CGSizeZero;
  _topViewController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.layer.bounds].CGPath;

    [self.view addSubview:_topViewController.view];
}

And on my initialviewcontroller...

self.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"Home"];

So it's just stacking on top, rather than switching to this view. So, its always listening to initial view controllers rotation methods...

Help is much appreciated, as i said i have been stuck for days...


Solution

  • After struggling for a few hours, finally i can make this work..

    First, you need to create a subview of ECSLidingViewController and put this code:

    -(NSUInteger)supportedInterfaceOrientations{
         return [self.topViewController supportedInterfaceOrientations]; 
     }
    
    -(BOOL)shouldAutorotate{
         return self.topViewController.shouldAutorotate;
     }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
         return [self.topViewController preferredInterfaceOrientationForPresentation];
     }
    

    You also need to create a category of UINavigationController and override this code

    @implementation UINavigationController (Rotation_IOS6)
    
    -(BOOL)shouldAutorotate{
         return [[self.viewControllers lastObject] shouldAutorotate];
     }
    
    -(NSUInteger)supportedInterfaceOrientations{
         return [[self.viewControllers lastObject] supportedInterfaceOrientations];
     }
    
     - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
         return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
     }
    
    @end
    

    Hope this work for you..