ios - 带有选择器 View 的弹出窗口未正确显示

标签 ios cocos2d-iphone

这是我尝试打开其中包含 2 个选择器 View 的弹出窗口时的代码。

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

上图是我的弹出窗口无法正常工作时的样子,下图是它正常工作时的样子。

最佳答案

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

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

你可能想要的是界面的当前方向,你得到 通过调用当前 View Controller 的 interfaceOrientation 方法。 它有 4 个可能的值

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

关于ios - 带有选择器 View 的弹出窗口未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21657513/

相关文章:

iphone - Cocos2d的touch locationInView、covertToGL、convertToNodeSpace有什么区别?

iphone - 随机 Sprite 目的地

iOS json识别

ios - Cocos2d 纵向模式下的偏移问题

ios - 使用Cocos2d,检测Sprite是否被点击,我们需要做所有的计算?

ios - CCSprite 没有出现在 CCLayerColor 中

ios - 在 Swift 中解析 twitter api JSON

ios - 调用 API 花费太多时间来获得响应的问题

ios - 执行 react-native run-ios 时出错

ios - Ionic v3 从 PLIST 中删除特定键