Appleのドキュメントによると、「サブクラスが他のビューのコンテナである場合、サブクラスは-[UIView drawRect:]をオーバーライドする必要はありません。」
私はカスタムUIViewサブクラスを持っていますが、これは実際には他のビューの単なるコンテナーです。しかし、含まれているビューは描画されていません。カスタムUIViewサブクラスを設定する適切なコードは次のとおりです。
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Consists of both an "on" light and an "off" light. We flick between the two depending upon our state.
self.onLight = [[[LoyaltyCardNumberView alloc] initWithFrame:frame] autorelease];
self.onLight.backgroundColor = [UIColor clearColor];
self.onLight.on = YES;
[self addSubview:self.onLight];
self.offLight = [[[LoyaltyCardNumberView alloc] initWithFrame:frame] autorelease];
self.offLight.backgroundColor = [UIColor clearColor];
self.offLight.on = NO;
[self addSubview:self.offLight];
self.on = NO;
}
return self;
}
このカスタムUIViewを表示するコードを実行すると、何も表示されません。しかし、drawRectメソッドを追加すると...
- (void)drawRect:(CGRect)rect
{
[self.onLight drawRect:rect];
[self.offLight drawRect:rect];
}
...サブビューが表示されます。(明らかに、これはこれを行うための正しい方法ではありません。これは、ドキュメントの記述に反しているだけでなく、常に両方のサブビューを表示し、UIView内の隠されたプロパティを設定する他のコードを完全に無視しているためです。ビューの1つで、z順序などを無視します。)
とにかく、主な質問:drawRectをオーバーライドしていないのにサブビューが表示されないのはなぜですか?
ありがとう!
アップデート:
問題がカスタムサブビューにないことを確認するために、UILabelも追加しました。したがって、コードは次のようになります。
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Consists of both an "on" light and an "off" light. We flick between the two depending upon our state.
self.onLight = [[[LoyaltyCardNumberView alloc] initWithFrame:frame] autorelease];
self.onLight.backgroundColor = [UIColor clearColor];
self.onLight.on = YES;
[self addSubview:self.onLight];
self.offLight = [[[LoyaltyCardNumberView alloc] initWithFrame:frame] autorelease];
self.offLight.backgroundColor = [UIColor clearColor];
self.offLight.on = NO;
[self addSubview:self.offLight];
self.on = NO;
UILabel* xLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
xLabel.text = @"X";
[self addSubview:xLabel];
}
return self;
「X」も表示されません。
更新2:
カスタムUIView(OffOnLightView)を呼び出すコードは次のとおりです。
// Container for all of the OffOnLightViews...
self.stampSuperView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
[self.view addSubview:self.stampSuperView];
// Draw the stamps into the 'stamp superview'.
NSInteger numberOfCardSpaces = (awardType == None) ? 3 : 10;
for (NSInteger i = 1; i <= numberOfCardSpaces; i++)
{
OffOnLightView* newNumberView = [[[OffOnLightView alloc] initWithFrame:[self frameForStampWithOrdinal:i awardType:awardType]] autorelease];
newNumberView.on = (i <= self.place.checkInCount.intValue);
newNumberView.number = [NSString stringWithFormat:@"%d", i];
[self.stampSuperView addSubview:newNumberView];
}