iOS6.0 Pushing new viewController in portrait orientation from landscape viewController
Asked Answered
L

2

0

My application is navigation based and mainly runs in portrait orientation, But one view controller supports both portrait and landscape orientation. Now the problem is, When I push the new viewcontroller from landscape view controller, the new view controller is also pushed in landscape mode though i want it in portrait mode.

Also when I pop view controller from landscape controller then the poped view controller is getting displayed in portrait mode.

I don't know what is wrong with my code.

Here is the my code snippet and information used for these orientation support.

In info.plist file I kept support for all orientation but portrait upsidedown.

I have also added category for navigation controller category as below.

@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

I have also created a UIViewController subclass that acts as superclass for all classes. Here are the orientation methods for super class.

@implementation ParentViewController

- (BOOL)shouldAutorotate{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}
@end

And the orientation methods controller that supports landscape are as below.

@implementation LandscapeController
#pragma mark -
#pragma mark Orientation Methods
- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end

Thanks in advance.

Lighthouse answered 15/2, 2013 at 14:14 Comment(3)
i have one sample may be useful to u.Send ur email id i will send you thatBashan
Thanks Vidyanand for reply. Here is my email id : [email protected]Lighthouse
In my sample first view is portrait ,second view is landscape and third view is again portrait.delete ur emailBashan
B
0

Here is the method to push the view.take an object switch

At switch.m

+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease
{

VControllerToLoad.navigationController.navigationBar.frame=CGRectMake(0, 0, 568, 44);
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect windowFrame = [[UIScreen mainScreen] bounds];
CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
VControllerToLoad.view.frame = firstViewFrame;

//check version and go

if (IOS_OLDER_THAN_6)
{
    [((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:VControllerToLoad.view];
}
else
{
    [((AppDelegate*)[UIApplication sharedApplication].delegate).window setRootViewController:VControllerToLoad];

}
  [VControllerToRelease.view removeFromSuperview];
}

You can push to portrait view from landscape mode like this using above method

 PortraitViewController *portraitVC=[[PortraitViewController alloc]initWithNibName:@"PortraitViewController" bundle:nil];

  [Switch loadController:portraitVC andRelease:self];

At landscape view controller.m

    - (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.title=@"Landscape";
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.navigationController.navigationBarHidden = NO;
    if (IOS_OLDER_THAN_6)
        [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
    // Do any additional setup after loading the view from its nib.
}

#ifdef IOS_NEWER_OR_EQUAL_TO_6

-(BOOL)shouldAutorotate  
{
    return YES; 
 }

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

#endif
Bashan answered 15/2, 2013 at 14:45 Comment(0)
G
0

I have had a similar problem. In my case this very one view controller had to be displayed in landscape all times and on the way back the other view controllers must be set back to portrait.

I found the following solution in the FAQ: iPhone Landscape FAQ and Solutions

The key point of that idea is that the device can be forced into a certain orientation only by one way: 1. Set the orientation of the staus bar. 2. Present any view controller modally (!) that supports the targeted orientation.

Well, you do not want to present anything modally? Do not worry. Nothing stops you from ... 3. instantly dismissing that very view controller once it was presented. It will not even be visible to the user when you do it that way. But once that is done, your device remains in this very orientation. 4. Push the view controller that you want to display or even segue to it in the event that you use storyboards.

To me that seemed to be the only workable solution. In addition to that answer in the FAQ everything else must be set properly too. The supported device orientations for the full project must be basically all. The supported orientations for each view controlelr must be those, which each view conroller supports and shouldAutoRotate should return YES. It gets more difficult if a tab bar controller is involved. Get back to me if your app is based on a tab bar.

Glory answered 15/2, 2013 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.