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