0
在UIView
子類的初始化方法中,我添加了幾個UIImageViews
。渲染視圖時,它們出現在subviews
數組中,並且具有有效的框架矩形,但不在屏幕上繪製。有誰知道爲什麼發生這種情況?在初始化時向UIView添加子視圖
- (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");
}
可以發佈一些代碼或提供更多的精度。你所描述的一般方法應該可行,所以問題必須在於實現。 – amadour
上面加了代碼 –