iPhone:仅在横向模式下,第一次 addSubview 后,UITableViewController 无法正确旋转

标签 iphone landscape

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

在我的 UIWindow 上,当我添加第二个(及后续) UITableView 作为 subview 时,它们无法正确旋转,因此会出现在侧面。这仅在模拟器中进行了测试。这是给您的一些代码:

- (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 就会正确显示。

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

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

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

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

可能相关 - 且未得到解答 - SO 问题 herehere .

最佳答案

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

  1. 创建一个“主”UIViewController 子类,它实现 shouldAutorotate...,并将其添加为窗口上的唯一 View 。
  2. 要在 viewA 或 viewB 之间切换,请在此主视图 Controller 上组合使用missModalViewControllerAnimated: 和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. 所有方向更改都流经 View Controller ,而不是 View 。

  2. 据我所知,添加到窗口的第一个 View 或 subview 会得到一些特殊的效果。如果该 View 有 View Controller ,窗口就会计算出来。

也就是说,仅对于第一个 subview ,您可以想到以下代码:

[window addSubview:masterVC.view];

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

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

除此之外,我不明白更多的事情了。我发现我可以用第一个 subview 来做到这一点,但不能用其他 subview 来做到这一点,这一事实非常令人困惑。欢迎提供更多信息,请告诉我这是否对您有帮助。

关于iPhone:仅在横向模式下,第一次 addSubview 后,UITableViewController 无法正确旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1632117/

相关文章:

android - 如何在支持自然景观设备的情况下检测左景观(正常)与右景观(反向)?

swift - iOS 12 应用程序在 iPhone X/XS 的横向模式下与 UIImagePickerController 崩溃

ios - 为自定义 View Controller 覆盖非开放实例 swift

ios - CallKit (Call Directory Extension) 可以识别和阻止蜂窝电话吗

android - Android应用程序:MainActivity在横向->纵向模式下找不到ListView/崩溃

iphone - 如何使用 UIButton 上的代码解锁横向模式?

iphone - 将 UIScrollView 嵌套在 UIScrollView Cocoa Touch 中

ios - iPhone : How to detect the end of slider drag?

android - 如何使用 HTML5 访问 iPhone 或 Android 相机?

android - 如何将应用程序设置为仅纵向?