4

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

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

并更新了 Info.plist

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight

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

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

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

4

7 回答 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
于 2009-05-12T20:44:06.193 回答
3

我使用了推荐的代码。事实上,桌子确实会旋转。但是,标题栏仍处于正常的纵向方向和位置。如何重新定位标题栏?

//自定义出现旋转tableview

- (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);

}
于 2013-01-09T21:48:04.233 回答
1

我认为你需要实施

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

在您的 CharacterController 中也是如此。

于 2009-05-07T17:48:59.523 回答
1

找到另一个解决方案:

[myCurrentViewController presentModalViewController: newController animated: NO];
于 2009-05-11T04:04:37.773 回答
0

即使手机是横向的,您是否遇到过使用 UIInterfaceOrientationPortrait 开始调用 shouldAutorotateToInterfaceOrientation 的问题?

我有一个主要是纵向的应用程序,并且使用一个视图控制器,我提出了我确实想自动旋转的 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);
}
于 2009-10-28T19:12:34.947 回答
0

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

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

于 2011-03-15T20:22:14.443 回答
-2

Once again I found my own answer. LOL

After loading the new ViewController

self.view =  myCurrentViewController.view;

Update controller to orientate the new ViewController to landscape

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);
于 2009-05-07T19:38:29.060 回答