【发布时间】:2015-05-22 22:32:33
【问题描述】:
我正在尝试将子视图添加到子视图控制器的视图中。按下按钮时会创建子视图控制器。父视图控制器位于导航控制器内。
-(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];
}
在子视图控制器的 viewWillLayoutSubviews 方法中,我尝试通过调用下面的方法来正确定位子视图。
-(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;
}
}
}
}
子视图是标签,它们被添加到子视图控制器的视图中,但它们的位置不正确,我不知道为什么。我希望它们彼此堆叠,彼此居中并在子视图控制器中居中。它们相互堆叠并相互居中,但它们在子视图控制器中并不居中。
我尝试关注guide provided by Apple 并查看了网络上的其他地方,但我无法弄清楚。
【问题讨论】:
-
您的盘子最终是如何布置的? x 可以吗?
-
x 值是给我带来问题的原因。 y 值完美运行。
标签: ios objective-c cocoa-touch frame viewcontroller