7

我在 iOS 5.x 中动态进行方向检查,如下所示:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==         UIInterfaceOrientationPortraitUpsideDown) 
 {
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
// some more settings
}
else
{
self.profileNew.frame = CGRectMake(374, 540, 295, 58);
 // some more settings
}
 return YES;
}

对于 iOS 6,我做了以下代码,但那不起作用:

-(BOOL)shouldAutorotate{    
   return YES;
}

-(NSInteger)supportedInterfaceOrientations
{
 if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
  {
   self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
   // some more settings
  }
  else
  {
   self.profileNew.frame = CGRectMake(374, 540, 295, 58);
   // some more settings
  }
 return UIInterfaceOrientationMaskAll;
}

如何像在 iOS 5.x 中一样检查 iOS 6 中的界面方向?

谢谢

4

4 回答 4

9

您可以使用该viewWillLayoutSubviews方法,在其中检查方向

[[UIApplication sharedApplication] statusBarOrientation];

并相应地设置你的框架。

希望这可以帮助!

于 2012-12-20T08:43:52.917 回答
4

尝试在此方法中进行检查

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
                            duration:(NSTimeInterval)duration

编辑:

用这个:

BOOL isInPortrait = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]);
于 2012-12-20T08:44:11.893 回答
1

结合 Levi's 和 Nikola Kirev 的答案,现在代码可以正常工作。谢谢你们俩。这是其他人参考的代码:

对于 iOS 6:

 -(BOOL)shouldAutorotate{
    return YES;
  }


-(NSInteger)supportedInterfaceOrientations{
        if (UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
            self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
            //other codes            
        }

        else {
            self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
            //other codes
        }
        return UIInterfaceOrientationMaskAll;
       }

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
                            duration:(NSTimeInterval)duration{
    if (UIDeviceOrientationIsPortrait(orientation)) {
        self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
        //other codes
        self.profileNew.frame = CGRectMake(238, 713, 295, 73);            
    }

    else {
        self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
        //other codes
    }
}

对于 iOS 5.x 和 4.x

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
        //other codes      
    }

    else {
        self.viewProfile.frame = CGRectMake(374, 462, 295, 58); 
        //other codes
        }
    return YES;
    } 
}
于 2012-12-21T04:16:55.727 回答
0

试试这个 :

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]
于 2012-12-20T08:47:48.587 回答