【问题标题】:iOS - Adding Subviews to Child View Controller viewiOS - 将子视图添加到子视图控制器视图
【发布时间】: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


【解决方案1】:

不太确定我是否正确理解了您,但我猜您希望您的车牌位于视图的中心

(这就是驱使我来到这里的原因:

我希望它们彼此堆叠,彼此居中并在子视图控制器中居中。)

所以看起来你应该简单地替换

x = self.view.center.x;

x = 0.5f * CGRectGetWidth(self.view.frame);

【讨论】:

    【解决方案2】:

    我认为您需要在设置它们的中心之前将它们添加到视图中。设置中心将有效地设置视图框架相对于它的父级边界,并且在您设置它时它们没有父级。

    【讨论】:

    • 不幸的是,在设置它们的中心之前将它们添加到视图中并不能解决问题。
    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多