iOS - 将 subview 添加到 subview Controller View

标签 ios objective-c cocoa-touch frame viewcontroller

我正在尝试将 subview 添加到 subview Controller 的 View 中。按下按钮时会创建 subview Controller 。父 View Controller 位于导航 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];
}

在 subview Controller 的 viewWillLayoutSubviews 方法中,我尝试调用下面的方法,但未能正确定位 subview 。

-(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;
            }
        }
    }
}

subview 是标签,它们被添加到 subview Controller 的 View 中,但它们的位置不正确,我不明白为什么。我希望它们堆叠在一起,彼此居中并在 subview Controller 中居中。它们彼此堆叠并彼此居中,但在 subview Controller 中并不居中。

我尝试遵循guide provided by Apple并在网络上的其他地方查看过,但我无法弄清楚这一点。

最佳答案

不太确定我是否理解正确,但我猜你希望你的盘子位于 View 的中心

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

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);

关于iOS - 将 subview 添加到 subview Controller View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30406794/

相关文章:

iphone - IBAction - 在连接到服务器之前做一些事情

iphone 开发 : using touch down and touch up inside simultaneosly

objective-c - 将文本字段保持在键盘上方

iphone - 使用 NSFetchedResultsController 处理背景变化

ios - SpriteKit Sprite 未正确缩放

iphone - 使用 AFNetworking 解析 XML 文件

ios - 如何使用 objective-c 将图像存储在缓存内存中

iphone - Cocoa/iOS/iPhone 中的模型对象所有权和 MVC

objective-c - NSObjectController 混淆绑定(bind)到类属性。帮助!

ios - NSUserDefaults 在特定时间后清除其值