所以我正在开发一个 iPad 应用程序,除了一个模态视图 Controller 外,它只支持横向模式。我遇到的问题是,一旦我呈现模态视图并将方向更改为纵向然后关闭 View ,父 View (应该只支持横向)处于纵向模式,直到我旋转设备然后返回到景观并保持这种状态。我一直在折磨自己,试图弄清楚如何让 parent 看到原来的方向,但一直未能找到解决办法。
我的应用程序委托(delegate)中有以下代码,仅允许在单个模态视图 (GalleryPhotoViewer) 上更改方向:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
//Support Portrait mode only on Photoviewer
if ([[presentedViewController presentedViewController] isKindOfClass:GalleryPhotoViewController.class] ) {
orientations = UIInterfaceOrientationMaskAll;
}else{
orientations = [presentedViewController supportedInterfaceOrientations];
}
}
return orientations;
}
我正在从父类 (PhotosViewController) 调用:
GalleryPhotoViewController *gpView = [GalleryPhotoViewController new];
[self presentViewController:gpView animated:YES completion:nil];
同样在我的 parent (和其他观点)中,我有以下代码来禁止纵向模式:
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
} else {
return NO;
}
}
关于如何保持父 View 方向的任何想法?我正在考虑可能只是在模态被关闭后以编程方式更改 viewWillAppear 方法中父级的方向但是我不知道以前的方向是什么,更不用说我无法找到代码来执行此操作不管 ios6.
编辑/解决方案:所以我找到了一个解决方案,我最终做的是离开 application:supportedInterfaceOrientationsForWindow: 代码,只是将 UINavigation 子类添加到呈现模态视图的父 View ,一切都按预期工作,父 View 保留其原始方向,而模态能够自由更改。
在我的 parent 中:
//To make sure that this view remains in Landscape
@implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
@end
感谢@matt 的建议。
最佳答案
我认为问题在于您对 application:supportedInterfaceOrientationsForWindow:
的使用。相反,摆脱它,从 UINavigationController 子类开始,并将其作为导航界面的 Root View Controller 的类。然后:
在 UINavigationController 子类中,从
supportedInterfaceOrientations
返回UIInterfaceOrientationMaskLandscape
。在呈现的(模态) View Controller 中,从
supportedInterfaceOrientations
返回UIInterfaceOrientationMaskAll
。
关于ios - 在以不同方向呈现模态视图后保持父 View 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329500/