iPhone横屏模式在加载新控制器时切换为竖屏模式

4

我的应用程序能正确地在横屏模式下启动并且工作得很好:

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

并更新了Info.plist文件:

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight

现在,在我的主控制器中,我将一个视图控制器替换为另一个视图控制器。
characterController = [ [ CharacterController alloc ] init];
myCurrentViewController = characterController;
self.view =  myCurrentViewController.view ;

当我加载视图控制器时,它能够正常加载但是方向是纵向模式。如果我旋转iPhone,它会自动调整到横向模式。有没有什么办法在将新的视图控制器加载到我的主控制器中时保持横向方向?

7个回答

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

是的,在我解决了问题并进一步阅读后,我发现应该使用UINavigationController。 - Charles Peterson

3
我使用了推荐的代码,确实可以旋转表格。但是标题栏仍然处于普通竖屏方向和位置。我该如何重新定位标题栏?
// 自定义外观旋转表视图
- (void)viewDidAppear:(BOOL)animated
{
    self.view.transform = CGAffineTransformMakeRotation(-3.14 * (90) / 180.0);
    self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
    self.view.center = CGPointMake(160.0f, 240.0f);

}

请使用CGAffineTransformMakeRotation(M_PI_2)代替,或者将3.14添加一些小数位。否则旋转角度会偏离0.05度,渲染时可能会出现一些锯齿状的元素。 - Sten

1

我认为你需要实现

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

同样在你的角色控制器中实现。


已经实施,但问题并未反映出来。我更新了问题。 - Charles Peterson

1
发现另一种解决方案:
[myCurrentViewController presentModalViewController: newController animated: NO];

0

你是否遇到过这样的问题,即使手机横向时,shouldAutorotateToInterfaceOrientation 也以 UIInterfaceOrientationPortrait 调用开始了?

我有一个主要只接受竖屏的应用程序,并且使用 presentModalViewController 呈现一个视图控制器,其中我确实希望进行自动旋转,而且已经很好地实现了。但是当我打开它时,它总是从竖屏开始。似乎该视图控制器更倾向于父视图控制器的方向而不是设备的方向。

以下是在模态视图的视图控制器中适用的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

// if device orientation is one of these, don't care what our interface orientation is
if (deviceOrientation == UIDeviceOrientationUnknown && deviceOrientation == UIDeviceOrientationFaceUp && deviceOrientation == UIDeviceOrientationFaceDown)
  return YES;

// otherwise only return YES for the ui orientation matching the current device orientation
return (interfaceOrientation == deviceOrientation);
}

0

我遇到了完全相同的问题,只不过我是将模态添加到UITableViewController中。我发现这只有在从viewDidLoad调用时才会出现问题,因为模态在视图控制器的委托确认方向之前被呈现。

所以我只是在viewDidLoad中使用了一个带有计时器的performSelector调用。现在它以正确的方向加载。


-2

又一次我找到了自己的答案。哈哈

加载新的 ViewController 后

self.view =  myCurrentViewController.view;

更新控制器以将新的视图控制器定位为横向

self.view.transform = CGAffineTransformMakeRotation(-3.14 * (90) / 180.0);
self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
self.view.center = CGPointMake(160.0f, 240.0f);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接