Reputation: 111
I am trying to add subviews to the view of a child view controller. The child view controller is created when a button is pressed. The parent view controller is inside a navigation controller.
-(void)weightPlatePressed:(id)sender {
NSInteger week = self.weekControl.selectedSegmentIndex + 1;
NSInteger set = self.setControl.selectedSegmentIndex + 1;
NSInteger max = [self.weightTextField.text integerValue];
NSInteger weight = [KFLiftCalculator weightForWeek:week andSet:set fromMax:max];
if (weight <= 0) {
return;
}
KFWeightPlateViewController * vc = [[KFWeightPlateViewController alloc] init];
vc.delegate = self;
if (self.outputUnitControl.selectedSegmentIndex == 0) {
vc.weightPlatesArray = [KFLiftCalculator plateCountsInPoundsWeight:weight];
vc.isPoundsUnit = YES;
} else {
vc.weightPlatesArray = [KFLiftCalculator plateCountsInKilosForWeight:weight];
vc.isPoundsUnit = NO;
}
[self addChildViewController:vc];
CGFloat width = self.view.frame.size.width * 0.8;
vc.view.frame = CGRectMake(1, 1, width, width * 1.2);
vc.view.center = self.view.center;
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
}
In the viewWillLayoutSubviews method of the child view controller I try but fail to correctly position subviews by calling the method below.
-(void)addPoundWeightPlates {
UILabel * currentPlate = nil;
UILabel * previousPlate = nil;
const CGFloat scaleFactor = (self.view.frame.size.width / 40);
const CGFloat bottomPadding = 10;
for (NSInteger i = 0; i < [self.weightPlatesArray count]; i++) {
NSInteger numberOfPlates = [[self.weightPlatesArray objectAtIndex:i] integerValue];
if (numberOfPlates > 0) {
for (NSInteger j = 0; j < numberOfPlates; j++) {
currentPlate = [KFWeightPlate poundWeightPlateNumber:i scaleFactor:scaleFactor];
CGFloat x, y;
if (previousPlate == nil) {
y = self.view.frame.size.height - bottomPadding - (currentPlate.frame.size.height/2);
} else {
y = previousPlate.center.y - (previousPlate.frame.size.height/2) - (currentPlate.frame.size.height/2);
}
x = self.view.center.x;
currentPlate.center = CGPointMake(x, y);
[self.view addSubview:currentPlate];
previousPlate = currentPlate;
}
}
}
}
The subviews are labels and they get added to the child view controller's view but they are not positioned correctly and I can not figure out why. I want them stacked on top of each other, be centered on each other and be centered in the child view controller. They are stacked on each other and centered on each other but they are not centered in the child view controller.
I have tried to follow the guide provided by Apple and have looked elsewhere around the web but I can not figure this out.
Upvotes: 0
Views: 560
Reputation: 6385
Not quite sure I understood you correctly, but my guess is that you want you plates to be located in the center of your view
(that's what drove me here:
I want them stacked on top of each other, be centered on each other and be centered in the child view controller.)
So it looks like you should simply replace
x = self.view.center.x;
with
x = 0.5f * CGRectGetWidth(self.view.frame);
Upvotes: 1
Reputation: 4955
I think you need to add the plates to your view before setting their center. Setting center will effectively set the views frame with respect to it's parent's bounds and they don't have a parent when you're setting it.
Upvotes: 0