views:

61

answers:

1

I'm trying to create my own UIView subclass. I've placed it on my view in Interface Builder by dragging out a UIView, then specifying my subclass's name in the Class Identity field. Yet my UIView does not draw itself.

Here--in a simplified example--is the code in my UIView subclass:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        UILabel* label = [[[UILabel alloc] initWithFrame:self.frame] autorelease];
        label.text = @"Hello.";
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];        
    }

    return self;
}

I've seen reference to overriding drawRect:, but honestly I have no idea what I'd do in that method. I'm sure I'm doing something obviously wrong, but I have no idea what.

Any suggestions would be greatly appreciated!

Thanks.

+1  A: 

Try using self.bounds instead of self.frame:

UILabel* label = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];

Your view's frame is probably not at origin {0 0}, which means that the label would end up outside your view's visible area.

Thomas Müller
That was it! Thanks.
Greg Maletic