在UIViewController中嵌入一个UIViewControllers数组

时间:2016-07-03 03:04:40

标签: ios objective-c iphone uiview uiviewcontroller

我正在尝试在UIViewController内嵌入一个UIViewControllers数组,其中UIViewControllers视图具有特定的大小& UIViewController视图中的位置。我发现的是子UIViewControllers,他们的视图除了原点(0,0)之外没有出现在屏幕上。在父ViewController中,我添加了子视图以适应子控制器的拼贴。在子ViewControllers中变得明显,视图框架具有在父ViewController中声明的原始偏移。我试图为每个子视图的顶部/左侧和尺寸引入一些约束,但这似乎没有什么区别。 我在这个主题上看到的所有建议似乎只显示了一个子视图控制器的例子。

- (void)viewWillAppear:(BOOL)animated
{
    NSUInteger i = 0;
    for(PlotInfo *plotinfo in _plot.plots)
    {
        switch([plotinfo.PlotType integerValue])
        {
            case SWPlotTypesScatterPlot:
            {
                NSUInteger index = [self indexInViewControllers:scatterPlotViewControllers ForPartition:[plotinfo.PartitionIndex integerValue]];
                if(index == NSNotFound)
                {
                    ScatterPlotViewController *scatterPlotViewController = (ScatterPlotViewController*)[storyboard instantiateViewControllerWithIdentifier:@"segueScatterPlotView"];
                    scatterPlotViewController.currentPlot = currentPlot;
                    scatterPlotViewController.delegate = self.delegateScatterPlot;
                    plotViewController = scatterPlotViewController;

                    [scatterPlotViewControllers addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:scatterPlotViewController, kViewController, plotinfo.PartitionIndex, kPartitionIndex, [NSMutableArray arrayWithObject:[NSNumber numberWithInteger:i]], kActiveIDs, nil]];

                    scatterPlotViewController.view.frame = [self frameForChildController:[plotinfo.PartitionIndex integerValue]];
                    [self.view addSubview:scatterPlotViewController.view];
                    [self addChildViewController:scatterPlotViewController];

                    [self initConstraintsToSubView:scatterPlotViewController.view];


    //                    scatterPlotViewController.view.frame = CGRectMake(0.0, 0.0, scatterPlotViewController.view.frame.size.width, scatterPlotViewController.view.frame.size.height);
                    }
                    else
                    {
                        NSMutableDictionary *dict = (NSMutableDictionary*)[scatterPlotViewControllers objectAtIndex:index];
                        NSMutableArray *newActiveIDs = [NSMutableArray arrayWithArray:(NSMutableArray*)[dict objectForKey:kActiveIDs]];
                        [newActiveIDs addObject:[NSNumber numberWithInteger:i]];
                        [dict setObject:newActiveIDs forKey:kActiveIDs];
                    }
                }
                    break;
    // ………….                
                default:
                    break;
            }
            i++;
        }

        if([scatterPlotViewControllers count] > 0)
        {
            for(NSDictionary *dict in scatterPlotViewControllers)
            {
                ScatterPlotViewController *scatterPlotViewController = (ScatterPlotViewController*)[dict objectForKey:kViewController];
                scatterPlotViewController.activeIDs = [NSArray arrayWithArray:(NSMutableArray*)[dict objectForKey:kActiveIDs]];
                [scatterPlotViewController didMoveToParentViewController:self];
            }
        }
        // ………….
        [self updateViewConstraints];
}

- (void)viewWillDisappear:(BOOL)animated
{
// ………..

    if([scatterPlotViewControllers count] > 0)
    {
        for(NSDictionary *dict in scatterPlotViewControllers)
        {
            ScatterPlotViewController *scatterPlotViewController = (ScatterPlotViewController*)[dict objectForKey:kViewController];
            [scatterPlotViewController willMoveToParentViewController:nil];
            [scatterPlotViewController.view removeFromSuperview];
            [scatterPlotViewController removeFromParentViewController];
        }
    }
//…………
    differentTypesOfPlots = nil;
    barChartViewControllers = nil;
    pieChartViewControllers = nil;
    polarPlotViewControllers = nil;
    scatterPlotViewControllers = nil;
}


- (CGRect)frameForChildController:(NSUInteger)partitionIndex
{
    CGRect frameRect = CGRectZero;
    if(partitionIndex < [plotPartitionInformation count])
    {
        PartitionCell *cell = (PartitionCell *)[plotPartitionInformation objectAtIndex:partitionIndex];
        CGFloat heightFactor = self.view.bounds.size.height / cell.refHeight;
        CGFloat widthFactor = self.view.bounds.size.width / cell.refWidth;
        frameRect = CGRectMake(cell.xorigin*widthFactor, cell.yorigin*heightFactor, cell.width*widthFactor, cell.height*heightFactor);
    }

    return frameRect;
}

- (NSUInteger)indexInViewControllers:(NSMutableArray*)array ForPartition:(NSUInteger)index
{
    return [array indexOfObjectPassingTest:
            ^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
            {
                return [(NSNumber*)[dict objectForKey:@"PartitionIndex"] integerValue] == index;
            }
            ];
}

- (void)initConstraintsToSubView:(UIView*)childView
{
    childView.translatesAutoresizingMaskIntoConstraints = NO;
    //Leading
    NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:childView.frame.origin.x];

    //Bottom
    NSLayoutConstraint *top =[NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:childView.frame.origin.y];

    //Height to be fixed for SubView same as AdHeight
    NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:childView.frame.size.height];
    //Height to be fixed for SubView same as AdHeight
    NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:childView.frame.size.width];

    [self.view addConstraint:leading];
    [self.view addConstraint:top];
    [childView addConstraint:height];
    [childView addConstraint:width];
}

1 个答案:

答案 0 :(得分:0)

似乎我在子ViewControllers视图中使用子代的self.view框架定义了图形子视图框架。当我将图形子视图的帧原点更改为(0,0)时,我解决了我的问题。

相关问题