简体   繁体   English

自动布局IOS 7故事板强制横向或纵向

[英]Autolayout IOS 7 Storyboard Force Landscape or Portrait

I have a single storyboard where a majority of the views should be in portrait mode but one should be in Landscape. 我只有一个情节提要板,其中大多数视图应为纵向模式,但其中之一应为横向模式。 I've down a significant amount of searching but keep coming up a little blank. 我进行了大量搜索,但一直空白。

My current implementation which isn't exactly seeming to work as I hoped consists of the following bits of code I've pieced together from this website: 我当前的实现似乎并没有像我希望的那样正常工作,它由我从本网站整理的以下代码组成:

I've made a RotationalViewController which extends from UINavigationController 我做了一个从UINavigationController扩展的RotationalViewController

#import "RotationControlledViewController.h"

@interface RotationControlledViewController ()

@end

@implementation RotationControlledViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

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

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    UIInterfaceOrientation ret =  [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    return ret;
}

@end

And then I've made a PortraitViewController 然后我做了一个PortraitViewController

#import "PortraitViewController.h"

@interface PortraitViewController ()

@end

@implementation PortraitViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

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

-(BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

and a LandscapeViewController 和一个LandscapeViewController

#import "LandscapeViewController.h"

@interface LandscapeViewController ()

@end

@implementation LandscapeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

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

-(BOOL)shouldAutorotate
{
return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

@end

I have played around with changing some of the settings but to no avail. 我一直在尝试更改某些设置,但无济于事。 My general plan of attack has been to make specific view controllers inherit from either LandscapeViewController or PortraitViewController if I want to lock them to a specific orientation. 我的总体攻击计划是,如果我想将特定的视图控制器锁定为特定方向,则可以使其从LandscapeViewController或PortraitViewController继承。

Things seem to be working for portrait view ( i can lock controllers into portrait view) but I seem unable to load my 1 landscape view and have it a) auto rotate to landscape upon load, or b) rotate to landscape at all. 事情似乎适用于纵向视图(我可以将控制器锁定为纵向视图),但是我似乎无法加载我的1个横向视图,并使其a)加载时自动旋转为横向,或b)完全旋转为横向。

Any suggestions would be very appreciated. 任何建议将不胜感激。

Applying the solution presented in: 应用以下解决方案:
Force controllers to Change their orientation in Either Portrait or Landscape 强制控制器以纵向或横向更改其方向

I was able to make things work. 我能够使事情正常。

In both my PortraiteViewController and LandscapeViewControllers I modified the viewDidLoad method to: 在我的PortraiteViewControllerLandscapeViewControllers中,我都将viewDidLoad方法修改为:

PortraiteViewController: PortraiteViewController:

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

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

    UIViewController *mVC = [[UIViewController alloc] init];
    [self presentModalViewController:mVC animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

LandscapeViewController: LandscapeViewController:

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

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

    UIViewController *mVC = [[UIViewController alloc] init];
    [self presentModalViewController:mVC animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

This solution MAY stop working after IOS7 because both presentModalViewController and dismissModalViewController are deprecated. 此解决方案可能在IOS7之后停止工作,因为不建议使用presentModalViewControllerdismissModalViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM