0

在子类的初始化方法中,UIView我添加了几个UIImageViews. 渲染视图时,它们存在于subviews数组中并具有有效的框架矩形,但不会在屏幕上绘制。有谁知道为什么会这样?

- (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");
}
4

1 回答 1

1

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

于 2012-10-04T23:59:07.783 回答