Alex Naspo
Alex Naspo

Reputation: 2092

iPad/iPhone orientation issue

I realize there a many questions relating to this issue, however i have not found one that solves my issue.

To start, I have set this code in my menu.h

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

The status bar changes with orientation but the views are not rotating or resizing. In order to try to narrow down my issue, I decided to try to switch two views within one .xib based on orientation

    - (void)viewDidLoad {
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
     }
    -(void) orientationChanged:(NSNotification *)object
{
   UIDeviceOrientation deviceOrientation = [[object object] orientation];

    if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == UIDeviceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}

In the iOS simulator, the view definitely changes to the specific views. However the landscape view shows up as if it was portrait and sideways.

my landscape view in IB enter image description here

My landscape view in the iOS simulator after changing orientation enter image description here

What am I doing wrong here? I can't figure it out, any help would be greatly appreciated. Thank you in advance. ** EDIT: New Code Below ** Okay.. my issue is that the view loads properly in landscape, it's just sideways. So the Landscape view loads, just sideways. My revised code based on @Seega is:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

    [[TTNavigator navigator] setCustomTarget:self];
    [[TTNavigator navigator] setCustomAction:@selector(photoAction)];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}

-(void) orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];

    if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == UIDeviceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}

Upvotes: 0

Views: 1633

Answers (5)

Alex Naspo
Alex Naspo

Reputation: 2092

Hey thank you all for your help. I had a friend help and am not entirely sure was the problem. I can tell you the things I know he did do.

  1. Disabled our ads temporarily

  2. deleted the second view entirely and went with native orientation and resizing

  3. added this to almost every .m file.

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    
    return YES;
    

    }

  4. As well a programmatically changed a few of the other views similar to this

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {
         if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight)
         {
              if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) 
              {         
                   self.Button.frame = CGRectMake(27,96,86,86);
                   self.Label.frame  = CGRectMake(27,162,86,21);
              }
             else 
             {
                   self.Button.frame = CGRectMake(18,100,86,86);
                   self.Label.frame  = CGRectMake(18,164,86,21);
              }
         }
     } 
    

Sorry, I can't fully explain the answer, I do not fully understand everything that was included in the project that affected the orientation. I am still very new to iOS development.

Upvotes: 0

RachelD
RachelD

Reputation: 4089

Idea!!!

If you are setting up the views in interface builder and then switching the actual view thats being displayed when the view rotates then you need to build that View in that particular orientation, not just size it to fit the orientation.

To check this:

  • Open your .xib in Interface Builder.
  • Click on the 'view' under objects so you have the whole view selected.
  • Look under the 'Simulated Metrics' on the right side of IB. Make sure 'Landscape' is selected in the 'Orientation' drop down.

If your view says 'Portrait' for the view you want to represent your landscapeView then it could be that xcode is rotating your landscape view to portrait and messing with your presentation.

Let me know if this helped.

Upvotes: 1

Seega
Seega

Reputation: 3390

You should better simply use

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
    else 
    {
        self.view = self.portraitView;
    }
}

to handle the rotations instead of creating a own one.
And now you can delete all the notification stuff.

Upvotes: 1

RachelD
RachelD

Reputation: 4089

I would suggest putting your code in :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

instead of making a new method. This will get called automatically if the shouldAutorotateToInterfaceOrientation method returns YES for that rotation.

Also there is a difference between UIDeviceOrientation and UIInterfaceOrientation so make sure your referencing the correct one. Your existing code would be changed to the following:

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    self.view = self.landscapeView;
}
else 
{
    self.view = self.portraitView;
}

Also you can use a macro to check the interface orientation keeping your code cleaner.

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
{
    self.view = self.landscapeView;
}
else 
{
    self.view = self.portraitView;
}

Upvotes: 4

Gopal Nair
Gopal Nair

Reputation: 840

Can you make sure, you have all the orientation support selected as follows. I tried this, and it seems to be working fine for me.

enter image description here

Upvotes: 1

Related Questions