在仅支持竖屏的应用中以横屏模式显示UIViewController

3

我的应用程序只支持竖屏模式。我需要在横屏模式下呈现一个UIViewController(原因是我正在使用Core-Plot SDK在该视图控制器上绘制图形,因此它需要处于横屏模式)。

我尝试了以下方法,它们都可以正常工作。但问题是,当我关闭横屏视图控制器时,应用程序无法强制回到竖屏模式。

http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

http://b2cloud.com.au/tutorial/forcing-uiviewcontroller-orientation/

如何在退出横屏视图控制器后强制应用程序仅为纵向模式

这是我展示横屏视图控制器并将其关闭的方式。

 LineChartViewController *obj = [[LineChartViewController alloc]initWithNibName:@"LineChartViewController" bundle:nil];

 [self.navigationController presentViewController:obj animated:YES completion:nil];  


- (IBAction)btnDonePressed:(id)sender{
    [self dismissViewControllerAnimated:NO completion:nil];
}

LineChartViewController的XIB处于横屏模式。

简单来说,我的应用只支持竖屏,但我想在横屏模式下显示CorePlot hostView。


我已经在我的一个应用程序中实现了这种功能,我的应用程序仅支持纵向模式,在我的应用程序中,我展示了一个横向模式的视图,并且它可以正常工作。我只是将NIB文件设置为横向模式,但用于呈现横向视图的方法不同。 - nivritgupta
使用此代码展示横向视图 LineChartViewController *obj = [[LineChartViewController alloc]initWithNibName:@"LineChartViewController" bundle:nil];[self.navigationController pushViewController:obj animated:YES]; - nivritgupta
我写了这个问题的答案:http://stackoverflow.com/a/30812053/893872 - Durul Dalkanat
4个回答

2

实际上,我可以通过旋转CorePlot的hostView来解决这个问题。虽然这个解决方案并不完美符合所描述的问题,但是我想在这里分享我的解决方案,因为它解决了我的问题。

self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.viewGraphBack.bounds];
self.hostView.allowPinchScaling = YES;
[self.viewGraphBack addSubview:self.hostView];

CGAffineTransform transform =
CGAffineTransformMakeRotation(DegreesToRadians(90));

viewGraphBack.transform = transform;

viewGraphBack.frame = CGRectMake(-285, 0, 568, 320);

[self.view addSubview:viewGraphBack];

1
如果父视图控制器处于UIInterfaceOrientationPortrait模式,而当前视图控制器应该处于UIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRight模式,则可以使用设备方向更改委托和一些代码来实现。在viewdidload中进行。

在ViewDidLoad中:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

& 在 ViewController 中实现 shouldAutorotateToInterfaceOrientation 代理。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) return YES;
    return NO;
}

像这样。确保从目标设置中勾选横屏模式。 在此输入图片描述

1
这种方法对我有效(暂时推送虚假的模型视图控制器),在引入新方向的其他视图控制器消失后调用:
- (void)doAWorkaroudnOnUnwantedRotation {
  // check if is workaround nesesery:
  if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    double delayInSeconds = 0.7;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
      UIViewController *fake = [[[UIViewController alloc] init] autorelease];
      UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
      [rootController presentModalViewController: fake animated: NO];
      [fake dismissModalViewControllerAnimated: NO];
    });
  }
}

0

在你的项目中写入这个类别

 #import "UINavigationController+ZCNavigationController.h"

@implementation UINavigationController (ZCNavigationController)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

在你需要横屏的视图控制器中

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接