ios - 在初始化时向 UIView 添加 subview

标签 ios ios5 uiview

UIView 子类的初始化方法中,我添加了几个 UIImageViews。渲染 View 时,它们出现在 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");
}

最佳答案

你的 UIImageview 可能是空的吗?您在哪里添加图像?

关于ios - 在初始化时向 UIView 添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12737623/

相关文章:

ios5 - iCloud 使用 Magical Record 显示 "Encountered an error while trying to respond to the roll of the baseline"

swift - 将具有透明度的 UIView(来自 xib)添加到 SceneKit

ios - Swift:UITableViewController 中的 PrototypeCells 问题

ios - 如何快速将图片放在图片之上

objective-c - 您如何创建一组实体,这些实体都与具有特定属性的其他实体有关系?在核心数据中

ios - 使用程序化自动布局时容器 View 的框架设置为 CGRectZero

ios - 为什么 UIView.animate(withDuration 没有效果?

ios - 异步调用 View 模型以重新加载数据后何时刷新 View UI

iphone - 显示相机胶卷等选项的 View 的名称是什么?

ios - TDD iOS 教程