在初始化时将子视图添加到UIView

时间:2012-10-04 23:50:51

标签: ios ios5 uiview

UIView子类的初始化方法中,我添加了几个UIImageViews。呈现视图时,它们出现在subviews数组中并具有有效的帧rects,但未在屏幕上绘制。有谁知道为什么会这样?

- (id) initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (!self)
        return nil;

    [self commonInit];

    return self;

}

- (void) commonInit {

    _padding = 128.0f;

    // Add the chord views here


    _chordImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"fn_maj_x_x_x_x_1_e_1_fn"], [UIImage imageNamed:@"en_maj_x_x_x_x_1_e_1_en"], [UIImage imageNamed:@"dn_maj_x_x_x_x_1_d_1_f#"], [UIImage imageNamed:@"an_min_x_x_x_x_1_a_1_en"], nil];

    CGFloat containerWidth = [self bounds].size.width;
    NSLog(@"containerWidth: %f",containerWidth);
    CGFloat chordWidth = [[_chordImages objectAtIndex:0] size].width;
    NSLog(@"chordWidth: %f",chordWidth);
    CGFloat chordMarkerWidth = [[[GVChordMarkerView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)] frame].size.width;
    NSLog(@"chordMarkerWidth: %f",chordMarkerWidth);

//  [self setFrame:CGRectMake(chordXPosition, 0, [self bounds].size.width, [self bounds].size.height]);

    CGFloat chordXPosition = 0;
    CGFloat chordXSpacing = (containerWidth/4 - (chordMarkerWidth + chordWidth));
    chordXPosition += containerWidth/4;

    for (NSUInteger i = 0; i < [_chordImages count]; i++) {
        chordXPosition += chordXSpacing;
        CGRect chordImageViewFrame = CGRectMake(chordXPosition, 0, [[_chordImages objectAtIndex:0] size].width, [[_chordImages objectAtIndex:0] size].height);
        UIImageView *chordImageView = [[UIImageView alloc] initWithFrame:chordImageViewFrame];
        [self addSubview:chordImageView];
        chordXPosition += (chordMarkerWidth + chordWidth);
    }
    NSLog(@"Finished");
}

1 个答案:

答案 0 :(得分:1)

难道你的UIImageview只是空的吗?你在哪里添加图片?