Objective c 需要帮助检查iPad的方位吗

Objective c 需要帮助检查iPad的方位吗,objective-c,cocoa-touch,ipad,uitableview,uiviewcontroller,Objective C,Cocoa Touch,Ipad,Uitableview,Uiviewcontroller,可能重复: 根据iPad所处的方向,我需要设置桌面视图的背景,或者删除它,因为它与popover不兼容 我试过了,但没有成功: if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){ self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:

可能重复:

根据iPad所处的方向,我需要设置桌面视图的背景,或者删除它,因为它与popover不兼容

我试过了,但没有成功:

if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
    self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
    self.tableView.backgroundColor = [UIColor clearColor];
    NSLog(@"test");

也许有更好的办法?若要查看表是master还是popover,然后设置背景?

有几种方法可以或多或少地实现您的目标:

UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
  • UIInterfaceOrientation方向=[[UIDevice currentDevice]方向]
  • 一种更老练的方法是检查状态栏的宽度
  • 如果选择选项1,则可以检查方向,如下所示:

    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            // Do something.
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            // Do something.
            break;
        case UIInterfaceOrientationLandscapeLeft:
            // Do something.
            break;
        case UIInterfaceOrientationLandscapeRight:
            // Do something.
            break;
    }
    
    编辑:要使应用程序自动旋转,可以执行以下操作:

    - (BOOL)willAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if ((interfaceOrientation == UIDeviceOrientationLandscapeRight)) NSLog(@"Right");
        if ((interfaceOrientation == UIDeviceOrientationLandscapeLeft)) NSLog(@"Left");
        if ((interfaceOrientation == UIDeviceOrientationPortrait)) NSLog(@"Up");
        if ((interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) NSLog(@"Down");
        return YES;
    }
    

    有几种方法可以或多或少地实现您的目标:

  • UIInterfaceOrientation方向=[[UIDevice currentDevice]方向]
  • 一种更老练的方法是检查状态栏的宽度
  • 如果选择选项1,则可以检查方向,如下所示:

    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            // Do something.
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            // Do something.
            break;
        case UIInterfaceOrientationLandscapeLeft:
            // Do something.
            break;
        case UIInterfaceOrientationLandscapeRight:
            // Do something.
            break;
    }
    
    编辑:要使应用程序自动旋转,可以执行以下操作:

    - (BOOL)willAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if ((interfaceOrientation == UIDeviceOrientationLandscapeRight)) NSLog(@"Right");
        if ((interfaceOrientation == UIDeviceOrientationLandscapeLeft)) NSLog(@"Left");
        if ((interfaceOrientation == UIDeviceOrientationPortrait)) NSLog(@"Up");
        if ((interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) NSLog(@"Down");
        return YES;
    }
    

    好的,如果我选择第一种。如何从该代码中检索方向,并根据方向使用if/else语句?如果使用选项一,可以。如何从代码中检索方向并根据方向使用if/else语句?这对我没有任何帮助。它没有获得方向?没有。我如何使用它。我只是在NSLog方向上加了这一行,什么也没有得到。我还需要它在每次方向改变时调用一个指定的方法。另一个答案包含一个switch语句,它应该会给你一个想法。这对我没有任何帮助。它没有得到方向?没有,我如何使用它。我只是在NSLog方向上加了这一行,什么也没有得到。我还需要它在每次方向改变时调用一个指定的方法。另一个答案包含一个switch语句,它应该会给你一个想法。