2014-02-09 37 views
0

這是我嘗試打開包含2個pickerviews的彈出窗口時的代碼。彈出式選取器視圖無法正確顯示

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

開頭的圖片是什麼樣子時,我酥料餅不能正常運行,底部是它正常工作等。

回答

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;