0

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;
    }
}
Alex Naspo
  • 2,052
  • 1
  • 20
  • 37
  • delete all the `[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];` stuff and the complete `-(void) orientationChanged:(NSNotification *)object` function. Use UIInterfaceOrientationLandscapeLeft and UIInterfaceOrientationLandscapeRight Additional just to check if the function works as expected change the color in the if statement instead of setting another UIView. – Seega Apr 16 '12 at 22:10
  • No luck on my end. I set it up as explained and entered breakpoints inside orientationChanged function, it is not being called whatsoever. – Alex Naspo Apr 16 '12 at 23:03
  • delete the damn orientationChanged: YOU DON'T NEED IT!!!! simply use `- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { self.view = self.landscapeView; } else { self.view = self.portraitView; } }` – Seega Apr 17 '12 at 09:20
  • This doesn't work. Before, the view at least changed, now it just doesn't work. – Alex Naspo Apr 17 '12 at 11:46

5 Answers5

4

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;
}
RachelD
  • 4,072
  • 9
  • 40
  • 68
  • Should I use `UIDeviceOrientation` or `UIInterfaceOrientation`? – Shamoon Apr 16 '12 at 19:15
  • You should use UIInterfaceOrientation because if your app only works in Portrait (for example) and the user rotates their device to landscape, now the app still looks like its in Portrait and is just being held side ways, when you check the UIDeviceOrientation it will be in landscape because the device is rotated but when you check UIInterfaceOrientation it will read as Portrait because the App itself did not rotate. Does this make sense? Also more details -> http://stackoverflow.com/questions/6838706/iphone-screen-rotation – RachelD Apr 16 '12 at 19:27
  • 1
    This makes sense, thank you. However, how do you set the app to rotate with the device? – Alex Naspo Apr 16 '12 at 19:33
  • 1
    Good question. I can't figure it out either – Shamoon Apr 16 '12 at 19:35
  • To set the app to rotate you need to (1) select the 'Supported Device Orientations' under the 'Summary' tab of your app (see the image posted by Gopal Nair on this page) and (2) you need to return YES; in the -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation method for any orientations you want to support for that ViewController (Note: each view controller can support different device orientations). – RachelD Apr 16 '12 at 19:54
  • I did these and double checked both. Are they're any other possible issues that could keep the app from rotating with my device? – Alex Naspo Apr 16 '12 at 19:59
  • Looking at your screen shots I can see that the device is rotating because nav bar is rotating. I think your issue is with setting the views. (1) Put a break point inside of your if statement to see what is being hit (the if or the else). (2) Where are you setting the variables landscapeView and portraitView? Are you sure there really being set? (3) Where are you setting the initial view of the view controller? – RachelD Apr 16 '12 at 20:04
  • `#import #import @interface IconsViewController : UIViewController { IBOutlet UIView *portraitView; IBOutlet UIView *landscapeView; UIDeviceOrientation orientation; } @property (nonatomic, retain) IBOutlet UIView *portraitView; @property (nonatomic, retain) IBOutlet UIView *landscapeView;` The initial view is also set to portraitView – Alex Naspo Apr 16 '12 at 20:09
1

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.

Seega
  • 3,001
  • 2
  • 34
  • 48
  • `- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; }` Is this what you had in mind? This doesn't work for me. Thanks for the help – Alex Naspo Apr 16 '12 at 18:26
  • The problem with the above code is that UIDeviceOrientation and UIInterfaceOrientation are different so you are checking if the toInterfaceOrientation == a UIDeviceOrientation and they never will. Heres more info on the difference - http://stackoverflow.com/questions/6838706/iphone-screen-rotation – RachelD Apr 16 '12 at 18:43
  • Its a super easy typo that doesn't cause any warnings/errors so its even sneakier. – RachelD Apr 16 '12 at 19:24
  • never had it before, so didn't think about. But now there are your problem ;) – Seega Apr 16 '12 at 22:05
1

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

Gopal Nair
  • 830
  • 4
  • 7
1

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.

RachelD
  • 4,072
  • 9
  • 40
  • 68
0

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.

Vaibhav Sharma
  • 1,123
  • 10
  • 22
Alex Naspo
  • 2,052
  • 1
  • 20
  • 37