加载新控制器时,iPhone横向模式切换为人像模式 [英] Iphone Landscape mode switching to Portraite mode on loading new controller

查看:95
本文介绍了加载新控制器时,iPhone横向模式切换为人像模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app launches in landscape mode correctly and works great:

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

并使用以下信息更新了Info.plist

And updated Info.plist with

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight

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

Now in my main controller I switch out one ViewController for another

characterController = [ [ CharacterController alloc ] init];
myCurrentViewController = characterController;
self.view =  myCurrentViewController.view ;

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

and it loads but the orientation is in Portrait mode. If I then rotated the iPhone it corrects it to landscape mode. Any ideas how to keep landscape orientation when loading a new viewController into my mainController?

推荐答案

请特别注意您的self.view=otherVC.view方法. UIViewController用于管理单个视图.它并非旨在交换其视图(这就是为什么方向更改无法正常工作的原因).如果您的ViewController不在屏幕上,则在-didReceiveMemoryWarning这样的情况下这很重要.它将安静地转储其视图,然后在屏幕上重新显示时,从NIB重新加载视图(或重新运行-loadView).

Be very careful about your self.view=otherVC.view approaches. A UIViewController is intended to manage a single view. It's not designed to have its view swapped out (which is why your orientation changes aren't working). This matters in cases like -didReceiveMemoryWarning if your ViewController isn't on the screen. It will quietly dump its view, and when it comes back on screen, reload the view from the NIB (or re-run -loadView).

您的presentModalViewController:方法要好一些,尽管它不是模态视图如何工作的方式.至少让每个ViewController管理自己的视图.通常,您将在此处使用UITabBarController或UINavigationController.我认为您有某些理由要避免这些.

Your presentModalViewController: approach is somewhat better, though it's not how a modal view is built to work. It at least lets each ViewController manage its own view. Typically you would use a UITabBarController or UINavigationController here. I assume you have some reason you're avoiding these.

对此,我推荐的解决方案是将UIView添加到主视图控制器的视图中(作为IBOutlet或代码形式).您可以换入和换出那个视图,而不是换入和换出UIViewController的视图.我可能会继续使用UIViewController的子类来处理此问题,并使用模仿UITabBarController的方法.

My recommended solution to this would be to add a UIView to your main view controller's view (as an IBOutlet or in code). You swap that view in and out rather than swapping the UIViewController's view in and out. I'd probably go ahead and subclass UIViewController to handle this, with methods modeled after 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

这篇关于加载新控制器时,iPhone横向模式切换为人像模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆