gpt4 book ai didi

iphone - 如果你的 UIView 只是一个容器,需要重写drawrect吗?

转载 作者:行者123 更新时间:2023-12-03 20:36:10 25 4
gpt4 key购买 nike

根据 Apple 的文档,“如果子类是其他 View 的容器,则子类不需要覆盖 -[UIView drawRect:]。”

我有一个自定义的 UIView 子类,它实际上只是其他 View 的容器。然而所包含的 View 并没有被绘制出来。以下是设置自定义 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];
}

...显示 subview 。 (显然,这不是这样做的正确方法,不仅因为它与文档所说的相反,而且因为它总是显示两个 subview ,完全忽略我的 UIView 中设置隐藏属性的其他一些代码 View 之一,它忽略 z 顺序等)

无论如何,主要问题是:当我不重写drawRect时,为什么我的 subview 不显示:?

谢谢!

更新:

为了确保问题不在于我的自定义 subview ,我还添加了 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];
}

最佳答案

您的 subview 的框架应初始化为父 uiview 的边界。 subview 位于相对于父 View 框架的不同坐标系中。

self.onLight = [[[LoyaltyCardNumberView alloc] initWithFrame:self.bounds] autorelease];

关于iphone - 如果你的 UIView 只是一个容器,需要重写drawrect吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2962605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com