[英]iOS - Adding Subviews to Child View Controller view
我试图将子视图添加到子视图控制器的视图。 当按下按钮时,将创建子视图控制器。 父视图控制器位于导航控制器内部。
-(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;
}
}
}
}
子视图是标签,它们被添加到子视图控制器的视图中,但是它们的位置不正确,我不知道为什么。 我希望它们彼此堆叠,彼此居中并在子视图控制器中居中。 它们彼此堆叠并且彼此居中,但是它们不在子视图控制器中居中。
不太确定我是否正确理解了您,但是我猜您希望板块位于视图的中心
(这就是驱使我来到这里的原因:
我希望它们堆叠在一起,彼此居中,并在子视图控制器中居中。)
所以看起来您应该简单地更换
x = self.view.center.x;
同
x = 0.5f * CGRectGetWidth(self.view.frame);
我认为您需要先将印版添加到视图中,然后再设置其中心。 设置中心将有效地设置相对于父级边界的视图框架,并且在设置父级时没有父级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.