需要帮助检查iPad的方向

时间:2011-08-19 01:52:10

标签: objective-c cocoa-touch ipad uitableview uiviewcontroller

  

可能重复:
  iPhone orientation

根据iPad的方向,我需要设置表格视图的背景或删除它,因为它不适用于弹出窗口。

我试过这个但没有运气:

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

也许有更好的方法?要查看表是主表还是弹出表,请设置背景?

2 个答案:

答案 0 :(得分:4)

有多种方法可以实现您想要的,或多或少:

  1. UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
  2. 更多的hack-ish方法是检查状态栏的宽度。
  3. 如果你选择选项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;
    }
    

答案 1 :(得分:2)

UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;