2014-02-17 53 views
1

我有一个故事板,其中大部分视图应该是纵向模式,但一个应该处于横向模式。我减少了大量的搜索,但一直保持一个小小的空白。Autolayout IOS 7故事板强制横向或纵向

我当前实现,其不完全似乎在工作,我希望由我从这个网站拼凑下面的代码位:

我做了一个RotationalViewController延伸UINavigationController的

#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 

然后我做了一个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; 
} 

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 

我有一些改变的设置,但无济于事发挥各地。我的一般攻击计划是让特定的视图控制器从LandscapeViewController或PortraitViewController继承,如果我想锁定它们到一个特定的方向。

似乎纵向视图工作(我可以将控制器锁定到纵向视图),但我似乎无法加载我的1横向视图,并让它a)自动旋转以加载景观,或b)旋转到风景在所有。

任何建议将不胜感激。

+0

也许看看http://stackoverflow.com/questions/18957785/force-controllers-to-change-their-orientation-in-either-portrait-or-landscape?rq=1 – jackslash

+0

这工作(有点) 。我想我可以调整它不会崩溃。谢谢 – Jeef

+0

也许花一秒钟的时间回答你自己的问题,为你的社区的利益:) – jackslash

回答

1

应用解决方案中提出:
Force controllers to Change their orientation in Either Portrait or Landscape

我能够把事情的工作。

在我PortraiteViewControllerLandscapeViewControllers都我修改viewDidLoad方法来:

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:

- (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]; 
} 

此解决方案可能会在IOS7后停止工作,因为目前的ModalViewControllerdismissModalViewController已被弃用。

+0

我马上去看看。谢谢。 – Jeef