1

I have a problem with orientations in my app. Assume that I have two views (with dedicated view controllers):

  • first should be displayed in portrait (it is displayed correctly)
  • second should be displayed in landscape (it is not displayed correctly)

It is coarctated and displayed in portrait (like in second image below). When I rotate device horizontal and back to portrait everything is OK. But after pushing view it displays incorrectly (images below). How can I fix this?

enter image description here

I use CustomNavigationController whish inherits from UINavigatorControler and implements three methods:

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}

In application delegate I initializing controller in this way:

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];

First view controller implements orientation functions in this way:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationPortrait)
        return YES;

    return NO;
}

Second view controller implements orientation functions in this way:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (orientation == UIInterfaceOrientationLandscapeRight)
        return YES;

    return NO;
}
user3106462
  • 189
  • 5
  • 12

3 Answers3

1

hi declare a global variable BOOL isLandScape ;

initialize it as isLandScape=NO;


   - (NSUInteger)supportedInterfaceOrientations
    {
    return UIInterfaceOrientationMaskLandscapeRight;
  }


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{

  if ((orientation == UIInterfaceOrientationLandscapeRight)||(orientation == UIInterfaceOrientationLandscapeLeft))
{

      isLandScape=YES;

        return YES;

}

else

{

isLandScape=NO;

 return NO;

}

yourObject.frame=CGRectMake(isLandScape?0:0,isLandScape?0:0,isLandScape?1024:768,isLandScape?768:1024);


}
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
Charan Giri
  • 1,097
  • 1
  • 9
  • 15
1

Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need.

Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). Add the following method in this custom navigation controller (this if for landscape orientation but you can change to portrait too).

- (NSUInteger)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}

and add to your view controller that should rotate:

- (BOOL)shouldAutorotate
{
    return YES;
}

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

Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. Then, if you want to block some orientations for a particular window:

– application:supportedInterfaceOrientationsForWindow:
Community
  • 1
  • 1
nabrugir
  • 1,839
  • 2
  • 22
  • 38
0

To do this You can use this function:

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];

You can use this wherever you want, but in application delegate (in didFinishLaunchingWithOptions) i must put this code:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

It's works perfectly!

user3106462
  • 189
  • 5
  • 12