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