iPhone:仅在横向上,在第一个addSubview之后,UITableViewController无法正常旋转 [英] iPhone: In landscape-only, after first addSubview, UITableViewController doesn't rotate properly

查看:124
本文介绍了iPhone:仅在横向上,在第一个addSubview之后,UITableViewController无法正常旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A minimal illustrative Xcode project for this is available on github.

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

On my UIWindow, when I add second (and subsequent) UITableView's as subviews, they do not rotate properly, and thus appear sideways. This is only tested in the Simulator. Here's a little code for you:

- (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];
}

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

viewB appears sideways. Comment out the addSubview for viewB, and viewA appears correctly. Do that for viewA only, and viewB appears correctly.

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

I am not creating these UITableViewControllers via NIBs, though the UIWindow is.

如果你想知道,ShellTVC是一个UITableViewController,并实现了这个方法:

In case you are wondering, ShellTVC is-a UITableViewController, and implements this method:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

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

Also, I have set the UIInterfaceOrientation in the plist file to UIInterfaceOrientationLandscapeLeft.

可能相关 - 而且没有答案 - 提出问题此处和< a href =https://stackoverflow.com/questions/1371007>这里。

Probably related -- and unanswered -- SO questions here and here.

推荐答案

我认为我想出了一种方法 - 可能是正确的方法 - 来做到这一点。

I think I figured out a way -- possibly the right way -- to do this.


  1. 创建一个masterUIViewController子类,它实现了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];
}

为什么这样做?


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

  1. All orientation changes flow through view controllers, not views.

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

As far as I can tell, the first view or subview that you add to your window gets some special sauce. If that view has a view controller, the window figures it out.

也就是说,对于第一个子视图,你可以想到这段代码:

That is, for the first subview only, you can think of this code:

[window addSubview:masterVC.view];

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

as doing something like this (not a real method!):

[window addSubview:masterVC.view withViewController:masterVC];

我对此不了解。我发现我能用第一个子视图做到这一点,而不是其他的,非常令人困惑。欢迎更多信息,如果这对您有所帮助,请告诉我。

I don't understand any more about it than that. I find the fact that I can do this with the first subview, but not others, supremely perplexing. More info welcomed, and please let me know if this helped you or not.

这篇关于iPhone:仅在横向上,在第一个addSubview之后,UITableViewController无法正常旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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