1

我正在制作一个包含子视图控制器的自定义 UIViewController。它们以这种方式放置在主视图控制器中:

- (void) setHeaderViewController:(UIViewController *)vc
{
    [self setViewController:vc isHeader:YES];
    _headerViewController = vc;
}

- (void) setDetailViewController:(UIViewController *)vc
{
    [self setViewController:vc isHeader:NO];
    _detailViewController = vc;
}

- (void) setViewController:(UIViewController*) viewController isHeader:(BOOL)isHeader
{
    UIViewController* viewControllerRef = ((isHeader) ? _headerViewController : _detailViewController);
     
    if(viewControllerRef == viewController)
         return;
     
    const CGSize selfSize = self.view.frame.size;
    
    CGRect viewFrame;
    
    if (isHeader)
    {
        viewFrame = CGRectMake(0, 0, selfSize.width, 150);
    }
    else
    {
        viewFrame = CGRectMake(0, 150, selfSize.width, selfSize.height-150);
    }

    UIView* viewControllerView = viewController.view;
    
    [viewControllerView setFrame:viewFrame];
    [viewControllerView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | ((isHeader) ? UIViewAutoresizingFlexibleBottomMargin : UIViewAutoresizingFlexibleTopMargin))];
    
    [self.view addSubview:viewControllerView];
}

如果我在每个子视图控制器插槽内放置两个 UIViewController,则方法中定义的大小会受到尊重。但是,当放置一个 UITabBarController 时,它会变得贪婪并占据整个空间:

UIViewController *vc = [[WIFCustomerDetailHeaderViewController alloc] initWithNibName:@"WIFCustomerDetailHeaderViewController" bundle:nil];
[self setHeaderViewController:vc];

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = @"Personal Data";
vc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Personal Data" image:nil tag:0];
vc1.view.backgroundColor = [UIColor redColor];
vc1.view.frame = CGRectMake(0,400,100,100);

UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = @"Diagnosis";
vc2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Diagnosis" image:nil tag:0];
vc2.view.backgroundColor = [UIColor purpleColor];
vc2.view.frame = CGRectMake(0,500,50,50);

NSArray *tabs = [[NSArray alloc] initWithObjects:vc1, vc2, nil];

WIFCustomerDetailTabViewController *tvc = [[WIFCustomerDetailTabViewController alloc] init];
[tvc setViewControllers:tabs];

[self setDetailViewController:tvc];

有谁知道这里会发生什么?我找不到原因...

下面的图片..

两个子视图控制器 在此处输入图像描述

4

1 回答 1

2

UITabBarController 类的文档说,“您必须将此视图安装为窗口的根。与其他视图控制器不同,标签栏界面永远不应安装为另一个视图控制器的子级”

您似乎正在尝试一种特别不受支持的设计。

于 2012-10-26T12:13:02.857 回答