在仅限肖像应用程序中以横向模式呈现UIViewController

时间:2014-07-31 10:30:54

标签: ios objective-c iphone uiviewcontroller uiinterfaceorientation

我的应用只是一张肖像。我需要在Landscape模式下呈现一个UIViewController(原因是我使用Core-Plot sdk在该viewcontroller上绘制图形,所以它需要处于横向模式)。

我尝试了以下方法,它工作正常。但问题是,当我解除横向视图控制器时,app无法强制进入纵向模式。

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。

4 个答案:

答案 0 :(得分: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 :(得分: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];
    });
  }
}

答案 2 :(得分:1)

如果是UIInterfaceOrientationPortrait模式下的父视图控制器&当前的viewcontroller应该在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;
}

喜欢这个。确保必须从目标设置签入横向模式。 enter image description here

答案 3 :(得分: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

在你需要Landscape的viewcontroller中

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}