iPhone:仅在横向上,在第一个addSubview之后,UITableViewController无法正常旋转

Cla*_*ges 4 iphone landscape

github上提供了一个最小的说明性Xcode项目.

在我的UIWindow上,当我添加第二个(和后续的)UITableView作为子视图时,它们不能正确旋转,因此会出现在侧面.这仅在模拟器中测试.这里有一些代码给你:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    ShellTVC* viewA = [[ShellTVC alloc] initWithTitle:@"View A"];
    ShellTVC* viewB = [[ShellTVC alloc] initWithTitle:@"View B"];

    // The first subview added will rotate to landscape correctly. 
    // Any subsequent subview added will not.

    // You may try this by various commentings and rearranging of these two statements.

    [window addSubview:[viewA tableView]];
    [window addSubview:[viewB tableView]];

    [window makeKeyAndVisible];
}
Run Code Online (Sandbox Code Playgroud)

viewB出现在侧面.注释掉viewB的addSubview,viewA正确显示.仅对viewA执行此操作,并且viewB正确显示.

我不是通过NIB创建这些UITableViewControllers,尽管UIWindow是.

如果您想知道,ShellTVC是一个UITableViewController,并实现此方法:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Run Code Online (Sandbox Code Playgroud)

另外,我已将plist文件中的UIInterfaceOrientation设置为UIInterfaceOrientationLandscapeLeft.

可能相关 - 而且没有答案 - 这里这里的问题.

Cla*_*ges 8

我想我找到了一种方法 - 可能是正确的方法 - 来做到这一点.

  1. 创建一个"master"UIViewController子类,它实现了shouldAutorotate ...,并将其添加为窗口上的唯一视图.
  2. 要在viewA或viewB之间切换,请在此主视图控制器上使用dismissModalViewControllerAnimated:&presentModalViewController:animated:的组合.

这是一些代码:

// this doesn't really do much but implement shouldAutorotate...
@interface MasterViewController : UIViewController
@end

@implementation MasterViewController
- (BOOL) shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation {
     return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
@end

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
    MasterViewController* masterVC;
    UIViewController* activeTVC;
    UIViewController* onDeckTVC;
}
@end

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    UIViewController* masterVC = [[MasterViewController alloc] init];        
    activeTVC = [[ShellTVC alloc] initWithTitle:@"View A"];
    onDeckTVC = [[ShellTVC alloc] initWithTitle:@"View B"];
    [window addSubview:masterView.view];
    [window makeKeyAndVisible];
    [masterVC presentModalViewController:activeTVC animated:NO];
}

// you would call this to toggle between "View A" and "View B"
- (void)toggleTVC {
    UITableViewController *hold = activeTVC;
    activeTVC = onDeckTVC;
    onDeckTVC = hold;
    [masterVC dismissModalViewControllerAnimated:NO];   
    [masterVC presentModalViewController:activeTVC animated:NO];
}
Run Code Online (Sandbox Code Playgroud)

为什么这样做?

  1. 所有方向更改都流经视图控制器,而不是视图.

  2. 据我所知,你添加到窗口的第一个视图或子视图会有一些特殊的味道.如果该视图具有视图控制器,则窗口会将其计算出来.

也就是说,对于第一个子视图,您可以考虑以下代码:

[window addSubview:masterVC.view];
Run Code Online (Sandbox Code Playgroud)

做这样的事情(不是一个真正的方法!):

[window addSubview:masterVC.view withViewController:masterVC];
Run Code Online (Sandbox Code Playgroud)

我对它的了解不止于此.我发现我可以用第一个子视图执行此操作,而不是其他子视图,这非常令人困惑.欢迎更多信息,如果这对您有帮助,请告诉我.