Reputation: 105
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...
Upvotes: 0
Views: 759
Reputation: 26
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..
Upvotes: 1
Reputation: 69027
This is a common problem: if you instantiate a view controller, the add its view to another view controller´s view through addSubview
, then the first controller will not get any autorotation related called.
To cope with this use case, Apple added to iOS5 what is called as UIViewController
Containment, where you can add a child controller to another controller; then all relevant methods (viewWillAppear/Disappear
; autorotation methods etc. will be auto-routed to child controllers).
Here are the essential methods you might need to call:
addChildViewController:
removeFromParentViewController
willMoveToParentViewController:
didMoveToParentViewController:
Have a look at Implementing a Container View Controller for detailed info.
Here you can find a tutorial that will give you step-by-step instructions as to how to use containment.
Keep in mind that this will only work for iOS>5, so if you need to support iOS4, you are out of luck. In any case, you might try to build a workaround by relaying the relevant messages to your sub controllers. E.g., in a app of mine, this is what willAnimateRotationToInterfaceOrientation
and didRotateFromInterfaceOrientation
look like:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
duration:(NSTimeInterval)duration {
[self.mainButtonBarController willAnimateRotationToInterfaceOrientation:interfaceOrientation
duration:duration];
[self.boardController willAnimateRotationToInterfaceOrientation:interfaceOrientation
duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.mainButtonBarController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[self.boardController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
I.e., they simply forward the same message to the child controllers. You can do the same for other methods you need (shouldAutorotate...
, etc.)
Upvotes: 0