简体   繁体   English

在视图加载期间iOS 6中的设备方向处理?

[英]Device orientation handling in iOS 6 during view load?

I have a Tab bar application. 我有一个Tab栏应用程序。 I was using XCode 4.3.3. 我正在使用XCode 4.3.3。 I have upgraded to 4.5.2 with iOS6 stuffs. 我用iOS6的东西升级到了4.5.2。

My code in the shouldAutorotateToInterfaceOrientation for each view will check the current device orientation and place all the UI components properly. 我在每个视图的shouldAutorotateToInterfaceOrientation代码将检查当前的设备方向并正确放置所有UI组件。

But after upgrading to iOS 6, this code is not executing. 但升级到iOS 6后,此代码未执行。 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 viewLoadviewDidLoadviewWillAppear

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. 如果我想在视图加载期间检查方向,并在视图加载期间将UI组件放置到适当的位置,我该怎么办? Kindly give your suggestion. 请提出你的建议。

Thanks 谢谢

if you want to check which is current orientation, then you just write in prifix.pch below line 如果你想检查哪个是当前方向,那么你只需要写下prifix.pch下面的行

 #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! 快乐的编码!

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... 要检查ios6中的方向,您必须编写以下方法,并在info.plist中,您必须定义您想要的方向...

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

you manage Oriantation like this ways:- 你像这样管理Oriantation: -

-(BOOL)shouldAutorotate
{
    return YES;
}

and check every time in ViewWillApear device Oriantation like:- 并在ViewWillApear设备中每次检查Oriantation,如: -

- (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 UPDATE

likely people use checking device orientation like below way in putting this line in to ViewWillApear :- 可能人们使用检查设备orientation如下所示将此行放入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

    }

}

在viewDidLoad方法中,您需要检查相同的内容,因此您需要编写代码,除非它可能影响视图(如果它不在纵向上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM