iOS - 将子视图添加到子视图控制器视图
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;
}
}
}
}
子视图是标签,它们被添加到子视图控制器的视图中,但它们的位置不正确,我不明白为什么。我希望它们堆叠在一起,彼此居中并在子视图控制器中居中。它们彼此堆叠并彼此居中,但它们不在子视图控制器中居中。
我尝试遵循 guide provided by Apple 并查看了网络上的其他地方,但我无法弄清楚。
我认为您需要先将板块添加到您的视图中,然后再设置它们的中心。设置中心将有效地设置相对于其父级边界的视图框架,并且当您设置它时它们没有父级。
不太确定我是否理解正确,但我的猜测是您希望您的盘子位于视图的中心
(这就是驱使我来到这里的原因:
I want them stacked on top of each other, be centered on each other and be centered in the child view controller.)
所以看起来你应该简单地替换
x = self.view.center.x;
和
x = 0.5f * CGRectGetWidth(self.view.frame);
我正在尝试将子视图添加到子视图控制器的视图中。按下按钮时创建子视图控制器。父视图控制器在导航控制器内。
-(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 并查看了网络上的其他地方,但我无法弄清楚。
我认为您需要先将板块添加到您的视图中,然后再设置它们的中心。设置中心将有效地设置相对于其父级边界的视图框架,并且当您设置它时它们没有父级。
不太确定我是否理解正确,但我的猜测是您希望您的盘子位于视图的中心
(这就是驱使我来到这里的原因:
I want them stacked on top of each other, be centered on each other and be centered in the child view controller.)
所以看起来你应该简单地替换
x = self.view.center.x;
和
x = 0.5f * CGRectGetWidth(self.view.frame);