Returning from only landscape view to portrait app

社会主义新天地 提交于 2019-12-13 06:07:01

问题


I have an app that works only in portrait mode, however, there is one view that needs to support both portrait as well as landscape mode. Anytime I return from this view the rest of app mess up.

The view that needs to support both orientations contains webview that is used to play live stream.

Simple setting shouldAutorotateToInterfaceOrientation doesn't work. The trick with presenting modal view controller neither. The trick with removing and inserting root view doesn't work as well.

I am afraid to use [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait] (that actually works) as it is private api.


回答1:


I have an app that all works in portrait orientation and only one view with fullscreen photo supports landscape orientation. So i present this fullscreen view modally and in my FullscreenViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return ([[UIApplication sharedApplication] statusBarOrientation]);
}

hope this can be useful




回答2:


You could use [[UIDevice currentDevice] setOrientation:orientation] in viewWillAppear: or viewDidAppear: to force app to rotate to desired orientation. But, that's private call, and not sure if Apple would approve your app :)(mine got approved). Good Luck!




回答3:


Actually the method with removing and inserting first view of window views as mentioned here does work! iOS portrait only app returns from UIWebview youtube in landscape

I just need from some reason not to ask for first view in window subviews. I need to provide root view myself instead.

So my solution is to implement following methods in view controller that supports both orientations:

-(void)viewWillAppear:(BOOL)animated
{
    m_b_avoid_landscape_orinetation = NO;

    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    m_b_avoid_landscape_orinetation = YES;

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [[self getTabBar].view removeFromSuperview];
    [window addSubview:[self getTabBar].view];

    [super viewWillDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{
    if (m_b_avoid_landscape_orinetation)
        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    else
        return YES;
}

Where [self getTabBar] provides my custom tab bar that works as first view in window's subviews.

Edit So with iOS 6 it doesn't work anymore. I used solution with inserting and removing modal dialog as mentioned elsewhere with new supportedInterfaceOrientations method.

-(void)goBack
{
    m_b_avoid_landscpae_orinetation = YES;

    UIViewController *viewController = [[UIViewController alloc] init];
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController presentViewController:viewController animated:NO completion:^{
        [viewController dismissModalViewControllerAnimated:NO];
    }];

    [self.navigationController popViewControllerAnimated:YES];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (m_b_avoid_landscpae_orinetation)
        return UIInterfaceOrientationMaskPortrait;

    return UIInterfaceOrientationMaskAllButUpsideDown;
}



回答4:


//
//  ViewController.h
//  CustomNavigationController
//
//  Created by Durul Dalkanat on 11/05/15.
//  Copyright (c) 2015 Durul Dalkanat. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)showLandscapeViewButtonClicked:(id)sender;

@end

//
//  ViewController.m
//  CustomNavigationController
//
//  Created by Durul Dalkanat on 11/05/15.
//  Copyright (c) 2015 Durul Dalkanat. All rights reserved.
//

#import "ViewController.h"
#import "LandscapeView.h"
#import "CustomNavigationControllerViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}


- (IBAction)showLandscapeViewButtonClicked:(id)sender {

  LandscapeView *landscape = [[LandscapeView alloc]initWithNibName:@"LandscapeView" bundle:nil];
  CustomNavigationControllerViewController *nav = [[CustomNavigationControllerViewController alloc]initWithRootViewController:landscape];
  nav.navigationBarHidden = true;
  [self presentViewController:nav animated:YES completion:^{

  }];

}


#pragma mark handeling rotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
  return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
  return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
  BOOL atLeastIOS6 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0;
  if(atLeastIOS6)
  {
    return UIInterfaceOrientationMaskPortrait;
  }
  else{
    return UIInterfaceOrientationPortrait;
  }
}


@end

And Insert a New ViewController with Xib. After please change freeform.

//
//  LandscapeView.h
//  CustomNavigationController
//
//  Created by Durul Dalkanat on 11/05/15.
//  Copyright (c) 2015 Durul Dalkanat. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LandscapeView : UIViewController

@end
//
//  LandscapeView.m
//  CustomNavigationController
//
//  Created by Durul Dalkanat on 11/05/15.
//  Copyright (c) 2015 Durul Dalkanat. All rights reserved.
//

#import "LandscapeView.h"

@interface LandscapeView ()

@end

@implementation LandscapeView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

//
//  CustomNavigationControllerViewController.m
//
//
//  Created by Durul Dalkanat
//


#import <UIKit/UIKit.h>

@interface CustomNavigationControllerViewController : UINavigationController

@end

//
//  CustomNavigationControllerViewController.m
//
//
//  Created by Durul Dalkanat
//

#import "CustomNavigationControllerViewController.h"

@interface CustomNavigationControllerViewController ()

@end

@implementation CustomNavigationControllerViewController

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;

}




@end


来源:https://stackoverflow.com/questions/12742134/returning-from-only-landscape-view-to-portrait-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!