首页 文章

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

提问于
浏览
2

我的应用程序只是一个肖像 . 我需要在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/

如何在关闭横向视图控制器后强制应用程序变为 portrait only 模式?

这就是我如何呈现景观视图控制器并解除它 .

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处于横向模式 .

In simple words, My app is portrait only, and I wanted to show CorePlot hostView in landscape mode.

4 回答

  • 1

    在您的项目中写下此类别

    #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;
    }
    
  • 1

    实际上我可以通过旋转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];
    
  • 0

    这种解决方法适用于我(临时推送假模型视图控制器),在引入新方向的其他视图控制器被调用后被调用:

    - (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

    如果在 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

相关问题