Popover与选择器视图无法正确显示

时间:2014-02-09 09:42:05

标签: ios cocos2d-iphone

当我尝试打开一个包含2个pickerviews的popover时,这是我的代码。

-(void) showPopover {
    NSLog(@"Showing popover.");
    BOOL right = NO;
    BOOL detected = NO;
    int translate = 0;
    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
        NSLog(@"Device is now in Portrait Mode");
        translate += 600;
        detected = YES;
    }
    else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"Device is now in LandscapeLeft Mode ");
        detected = YES;
    }
    else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
        NSLog(@"Device is now in LandscapeRight Mode");
        right = YES;
        detected = YES;
    }
    else if([[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"Device is now in PortraitUpsideDown Mode");

        detected = YES;
    }
    if(detected == NO) {
        translate = 0;
        NSLog(@"FREAK ACCIDENT, MATRIX GLITCH");
        //right = YES;
    }

    if(right) translate += 600;
    NSLog(@"Translate is %i", translate);
    UIView *windowView =  [[UIApplication sharedApplication] keyWindow];

    UIViewController* popoverContent = [[UIViewController alloc]
                                        init];
    UIView* popoverView = [[UIView alloc]
                           initWithFrame:CGRectMake(0, 0, 600, 350)];

    popoverView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
    [pickerView setFrame:CGRectMake(0, 0, 150, 216)];
    [popoverView addSubview:pickerView];
    [secondPickerView setFrame:CGRectMake(150, 0, 450, 216)];
    [popoverView addSubview:secondPickerView];
    popoverContent.view = popoverView;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(200, 250, 200, 50)];
    [button setTitle:@"Go!" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [popoverView addSubview:button];

    popoverContent.contentSizeForViewInPopover =
    CGSizeMake(600, 300);


    popoverController = [[UIPopoverController alloc]
                         initWithContentViewController:popoverContent];


    [popoverController presentPopoverFromRect:CGRectMake(70 + translate, 512, 1, 1)
                                            inView:windowView
                          permittedArrowDirections:UIPopoverArrowDirectionDown
                                          animated:YES];


}

如果我的iPad正在放下,则会出现“矩阵故障”错误并导致我的弹出窗口无法正常工作。如果设备处于纵向模式,有时也会发生这种情况。

This is what it looks like when it doesn't function properly

This is what it looks like when it functions

顶部图片是我的popover无法正常工作时的样子,底部是否正常工作。

1 个答案:

答案 0 :(得分:3)

[[UIDevice currentDevice] orientation]为您提供物理方向 该设备有7个可能的值:

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

您可能想要的是界面的当前方向 通过调用当前视图控制器的interfaceOrientation方法。 它有4个可能的值

typedef enum : NSInteger {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;