我有一个界面,我想以横向启动。启动后,当用户将设备旋转为纵向时,我正在显示一个日视图日历。返回横向时,日历将被关闭。一切都很好,我的应用程序用户界面在横向方向正确显示,日历在纵向方向正确显示。
问题是用户是否在启动时将 iPhone 保持为横向。无论我做什么,我都无法在横向模式下使用我的用户界面启动它。我的 UIDeviceOrientationDidChangeNotification 方法触发了两次,第一次 [UIDevice currentDevice].orientation 是横向,第二次是纵向。最终结果是用户界面旋转到纵向模式并显示日视图。不是我想要的。我希望用户界面保持横向,直到用户将设备从横向物理旋转到纵向。
我不明白为什么当用户以纵向握持设备时,它会以横向 [UIDevice currentDevice].orientation 触发。
这是我的代码在 viewController 中的样子...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {
return NO;
}
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
showingCalendar = NO;
initializing=YES;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
-(void)didRotate:(NSNotification *)notification {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if ((deviceOrientation == UIDeviceOrientationPortrait) || (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)) {
if ((!showingCalendar) && (!initializing)) {
showingCalendar = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease];
navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
[self presentModalViewController:navigationController animated:YES];
}
}else if ((deviceOrientation == UIDeviceOrientationLandscapeRight) || (deviceOrientation == UIDeviceOrientationLandscapeLeft)) {
if (showingCalendar) {
showingCalendar = NO;
if (deviceOrientation == UIDeviceOrientationLandscapeRight){
[self dismissModalViewControllerAnimated:YES];
}else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
[self dismissModalViewControllerAnimated:YES];
}
}else {
initializing = NO;
}
}
}