【问题标题】:UIViewController containmentUIViewController 包含
【发布时间】:2012-09-05 17:14:37
【问题描述】:

我正在尝试在我的一个项目中使用 UIViewController 包含功能。该应用程序只能在横向模式下使用。

我将 UIViewController A 添加为 UIViewController B 的子视图,并将 A 的主视图添加为 之一的子视图B 的意见。我还在 B 中保存了对 A 的引用:

@interface BViewController : UIViewController

@property (retain, nonatomic) AViewController *aVC;

@end

@implementation BViewController : UIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"];
    [self addChildViewController:self.aVC];
    [self.myContainerView addSubview:self.aVC.view];
}

@end

我遇到的问题是横向方向没有得到尊重。我进行了一些调试并找到了解决方案,但我担心它并不理想,因为它更像是一种 hack:

在 B 中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"];
    [self addChildViewController:self.aVC];
    [self.myContainerView addSubview:self.aVC.view];
    [self.aVC didMoveToParentViewController:self];
}

在 A 中:

- (void)didMoveToParentViewController:(UIViewController *)parentVC
{
    // Interchange width and height
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.**height**, self.view.frame.size.**width**);
}

我错过了什么吗?

【问题讨论】:

    标签: ios cocoa-touch ios5 uiviewcontroller


    【解决方案1】:

    您的代码:

    self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:@"A"];
    [self addChildViewController:self.aVC];
    [self.myContainerView addSubview:self.aVC.view];
    

    总是错的。在将 didMoveToParentViewController: 添加到父控制器后,您必须将其发送到子控制器。在这里查看我的讨论:

    http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers

    至于轮换,很可能你做的太早了。当调用viewDidLoad 时,该应用程序以纵向开始并且尚未旋转到横向。我在这里给出这个问题的解决方案:

    http://www.apeth.com/iOSBook/ch19.html#_rotation

    请注意那里的建议,即您等到didRotateFromInterfaceOrientation: 完成设置视图的外观。我想你在这里可能会遇到我在那里描述的同样的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      相关资源
      最近更新 更多