3
votes

My app is a portrait only. I need to present a UIViewController in Landscape mode(Reason for that is I am using Core-Plot sdk for drawing graphs on that viewcontroller, so it needs to be in landscape mode).

I tried the following methods and it work fine. But the issue is, when I dismiss the landscape viewcontroller, app cannot force to portrait mode.

http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

http://b2cloud.com.au/tutorial/forcing-uiviewcontroller-orientation/

How can I force the app to become portrait only mode after dismiss the landscape viewcontroller?

This is How I presenting the landscape view controller and dismissing it.

 LineChartViewController *obj = [[LineChartViewController alloc]initWithNibName:@"LineChartViewController" bundle:nil];

 [self.navigationController presentViewController:obj animated:YES completion:nil];  


- (IBAction)btnDonePressed:(id)sender{
    [self dismissViewControllerAnimated:NO completion:nil];
}

XIB of the LineChartViewController is in landscape mode.

In simple words, My app is portrait only, and I wanted to show CorePlot hostView in landscape mode.

4
i already did this type of Functionality in one of my app, my app is portrait only and in my application i show one view in landscape Mode, and its working fine i Just set the NIB File in landscape Mode but the method using for presenting the landscapeview is differentnivritgupta
Use this for presenting the landscapeview LineChartViewController *obj = [[LineChartViewController alloc]initWithNibName:@"LineChartViewController" bundle:nil]; [self.navigationController pushViewController:obj animated:YES];nivritgupta
I wrote the answer this question: stackoverflow.com/a/30812053/893872Durul Dalkanat

4 Answers

2
votes

Actually I could solve the issue by rotating the CorePlot hostView. The solution isn't the perfect for the the question described, but I'd like to put my solution here, since it solved my problem

self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.viewGraphBack.bounds];
self.hostView.allowPinchScaling = YES;
[self.viewGraphBack addSubview:self.hostView];

CGAffineTransform transform =
CGAffineTransformMakeRotation(DegreesToRadians(90));

viewGraphBack.transform = transform;

viewGraphBack.frame = CGRectMake(-285, 0, 568, 320);

[self.view addSubview:viewGraphBack];
1
votes

This kind of workaround works for me (temporary pushing fake model view controller), called after other view controller which introduces new orientation is demised:

- (void)doAWorkaroudnOnUnwantedRotation {
  // check if is workaround nesesery:
  if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    double delayInSeconds = 0.7;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
      UIViewController *fake = [[[UIViewController alloc] init] autorelease];
      UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
      [rootController presentModalViewController: fake animated: NO];
      [fake dismissModalViewControllerAnimated: NO];
    });
  }
}
1
votes

If Parent viewcontroller in UIInterfaceOrientationPortrait mode & current viewcontroller should be in UIInterfaceOrientationLandscapeLeft or UIInterfaceOrientationLandscapeRight in that case you may use device orientation changing delegate & a few codes in viewdidload

In ViewDidLoad

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

& implement shouldAutorotateToInterfaceOrientation delegate in ViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) return YES;
    return NO;
}

Like this. Assure that landscape mode must be checked in from target settings. enter image description here

0
votes

Write this category in your project

 #import "UINavigationController+ZCNavigationController.h"

@implementation UINavigationController (ZCNavigationController)

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

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

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

And in your viewcontroller where you need Landscape

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}