gpt4 book ai didi

加载新 Controller 时,iPhone 横向模式切换为纵向模式

转载 作者:行者123 更新时间:2023-12-03 19:06:33 25 4
gpt4 key购买 nike

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

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

并更新了 Info.plist

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight

现在,在我的主 Controller 中,我将一个 ViewController 切换为另一个 ViewController

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

它加载了,但方向是纵向模式。如果我随后旋转 iPhone,它会将其纠正为横向模式。在将新的 viewController 加载到我的 mainController 中时如何保持横向有什么想法吗?

最佳答案

要非常小心您的 self.view=otherVC.view 方法。 UIViewController 旨在管理单个 View 。它的设计目的不是要交换其 View (这就是您的方向更改不起作用的原因)。如果您的 ViewController 不在屏幕上,这在诸如 -didReceiveMemoryWarning 之类的情况下很重要。它会悄悄地转储其 View ,当它返回到屏幕上时,从 NIB 重新加载 View (或重新运行 -loadView)。

您的 presentModalViewController: 方法稍好一些,尽管它不是模态视图的构建方式。它至少让每个 ViewController 管理自己的 View 。通常,您会在此处使用 UITabBarController 或 UINavigationController。我认为你有一些理由避免这些。

我推荐的解决方案是将 UIView 添加到主视图 Controller 的 View 中(作为 IBOutlet 或在代码中)。您将 View 换入和换出,而不是换入和换出 UIViewController 的 View 。我可能会继续使用 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

关于加载新 Controller 时,iPhone 横向模式切换为纵向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/835989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com