Device orientation handling in iOS 6 during view load?

最后都变了- 提交于 2020-02-21 05:39:09

问题


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


回答1:


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!




回答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..
   }



回答3:


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

    }

}



回答4:


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.



来源:https://stackoverflow.com/questions/14189828/device-orientation-handling-in-ios-6-during-view-load

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!