这是我尝试打开包含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放下,则会出现“矩阵故障”错误,并导致我的弹出窗口无法正常运行。如果设备处于纵向模式,有时也会发生这种情况。
上面的图片是我的弹出窗口无法正常运行时的样子,下面是它正常工作时的样子。
最佳答案
[[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;