Вграждане на масив от UIViewControllers в рамките на UIViewController

Опитвам се да вградя масив от UIViewControllers в рамките на UIViewController с изгледа 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];
}

person swainwri    schedule 03.07.2016    source източник
comment
Тази работа изцяло зависи от произхода, зададен за клетката. Координатите трябва да са относителни нула в пространството на родителския изглед. Не виждам дефиницията на подходящата структура, която да ме уведоми как сте създали тази първоначална стойност   -  person Feldur    schedule 03.07.2016
comment
Изходните стойности и измерения се определят в рутината „- (CGRect)frameForChildController:(NSUInteger)partitionIndex“, тези измерения се създават от другаде в моето приложение, но всички измерения и произходи се факторизират, за да се намерят в рамките на родителския self.view. граници и са нула относителни. Като проверка генерирах UIImage, използвайки произхода и размерите на всеки дъщерен изглед, и полученото изображение възпроизвежда колаж от изгледи, както бих искал да видя на екрана.   -  person swainwri    schedule 04.07.2016


Отговори (1)


Изглежда, че дефинирах рамката на графичния подизглед в моя дъщерен изглед ViewControllers с рамката self.view на детето. Когато променя произхода на рамката на подизгледа на графиката на (0,0), решавам проблема си.

person swainwri    schedule 04.07.2016