2

I have a Tab bar application. I was using XCode 4.3.3. I have upgraded to 4.5.2 with iOS6 stuffs.

My code in the shouldAutorotateToInterfaceOrientation for each view will check the current device orientation and place all the UI components properly.

But after upgrading to iOS 6, this code is not executing. I have tried for almost one day to fix this, but had no luck.

I have also tried

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; 

in viewLoad, viewDidLoad, viewWillAppear

What should I do if I want to check the orientation during the view load and place the UI components to appropriate places during view load. Kindly give your suggestion.

Thanks

Easwaramoorthy Kanagaraj
  • 3,925
  • 8
  • 36
  • 62

4 Answers4

11

if you want to check which is current orientation, then you just write in prifix.pch below line

 #define isPortrait [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown

then where you want to check orientation ,write condition like below

    if(isPortrait)
    {
       //portrait mode....
    }
    else
    {
       //landscape mode....
    }

let me know it is working or not...

Happy coding!

Mundi
  • 79,884
  • 17
  • 117
  • 140
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
2

To check orientation in ios6,you will have to write below methods and also in your info.plist you will have to define which orientation you want...

let me know whether it is working or not!!!

Happy Coding!!!!

   - (BOOL)shouldAutorotate{
     return NO;
   }

   - (NSUInteger)supportedInterfaceOrientations{
      return UIInterfaceOrientationMaskPortrait;//you can write here which ever orientation you want..
   }
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • I have added this code, but this didn't solved my problem. supportedInterfaceOrientations method is called when my viewController loaded, but there i am unable to find the orientation using UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; – Easwaramoorthy Kanagaraj Jan 07 '13 at 11:48
  • have you added your orientation in info.plist file???it is very important to add the orientations which you want to support in device – NiravPatel Jan 07 '13 at 11:49
  • That is also done, but my problem is to find the interface orientation during the view load. Previously i was doing my required stuffs in shouldAutorotateToInterfaceOrientation. Now there is no such delegate method.. :( – Easwaramoorthy Kanagaraj Jan 07 '13 at 11:51
2

you manage Oriantation like this ways:-

-(BOOL)shouldAutorotate
{
    return YES;
}

and check every time in ViewWillApear device Oriantation like:-

- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
        if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
        {
            if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {

              //set your landscap View Frame
                [self supportedInterfaceOrientations];

            }



        }
        else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        {
            if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
      //set your Potrait View Frame
                [self supportedInterfaceOrientations];

            }
        }
        // Handle rotation
    }


    -(void)viewWillAppear:(BOOL)animated
    {
        [self willRotateToOrientation:[[UIDevice currentDevice] orientation]];  
        [super viewWillAppear:YES];
    }

UPDATE

likely people use checking deviceorientation like below way in putting this line in to ViewWillApear:-

[[UIApplication sharedApplication] statusBarOrientation];
    [[UIDevice currentDevice] orientation];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];

and

-(void)deviceRotated:(NSNotification*)notification
{

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        //Do your stuff for landscap
    }
    else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
      //Do your stuff for potrait

    }

}
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
0

At viewDidLoad method you need to check for the same and accordingly you need to write code unless it may affect view if it not in on portrait.

Milan Kamilya
  • 2,188
  • 1
  • 31
  • 44