Iphone横向模式在加载新控制器时切换到Portraite模式

Cha*_*son 4 iphone landscape orientation uiviewcontroller

我的应用程序正确地以横向模式启动并且运行良好:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait)
        return NO;
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft )
        return YES; 
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Run Code Online (Sandbox Code Playgroud)

并更新Info.plist

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight
Run Code Online (Sandbox Code Playgroud)

现在在我的主控制器中,我将一个ViewController换成另一个

characterController = [ [ CharacterController alloc ] init];
myCurrentViewController = characterController;
self.view =  myCurrentViewController.view ;
Run Code Online (Sandbox Code Playgroud)

它加载但方向处于纵向模式.如果我然后旋转了iPhone,它会将其更正为横向模式.任何想法如何在将新的viewController加载到我的mainController时保持横向方向?

Rob*_*ier 7

你的self.view=otherVC.view方法要非常小心.UIViewController旨在管理单个视图.它不是为了让它的视图换掉(这就是你的方向改变不起作用的原因).这种情况很重要,例如-didReceiveMemoryWarning你的ViewController不在屏幕上.它将悄悄地转储其视图,当它返回到屏幕上时,从NIB重新加载视图(或重新运行-loadView).

您的presentModalViewController:方法稍好一些,但不是如何构建模态视图才能工作.它至少让每个ViewController管理自己的视图.通常,您可以在此处使用UITabBarController或UINavigationController.我假设你有一些理由要避免这些.

我建议的解决方案是将UIView添加到主视图控制器的视图中(作为IBOutlet或代码).您可以交换视图,而不是交换UIViewController的视图.我可能会继续使用UIViewController来处理这个问题,方法是在UITabBarController之后建模.

@interface RNSwappableViewController : UIViewController
{
    ...
}
@property(nonatomic, assign) id<RNSwappableViewControllerDelegate> delegate;
@property(nonatomic) NSUInteger selectedIndex;
@property(nonatomic, assign) UIViewController *selectedViewController
@property(nonatomic, copy) NSArray *viewControllers;
@end

@protocol RNSwappableViewControllerDelegate : NSObject
- (void)swappableViewController:(RNSwappableViewController *)swappableViewController didSelectViewController:(UIViewController *)viewController
@end
Run Code Online (Sandbox Code Playgroud)