Device orientation handling in iOS 6 during view l

2019-04-12 21:33发布

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

4条回答
Evening l夕情丶
2楼-- · 2019-04-12 22:06

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

    }

}
查看更多
Juvenile、少年°
3楼-- · 2019-04-12 22:13

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..
   }
查看更多
何必那么认真
4楼-- · 2019-04-12 22:17

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.

查看更多
迷人小祖宗
5楼-- · 2019-04-12 22:20

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!

查看更多
登录 后发表回答