views:

265

answers:

1

I have a subclass of UIButton called INMenuCard and I am overriding the initWithFrame to include an activity indicator. The menuCard places correctly but any internal reference to "frame" give me "inf,inf,0,0" which means my activityIndicator subview is not placed correctly. What might I be missing?

@implementation INMenuCard

- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) 
    {

        CGRect innerFrame = CGRectInset(frame, 50.0f, 100.0f);
        activityIndicator = [[UIActivityIndicatorView alloc]
                             initWithFrame:innerFrame];
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        [self addSubview:activityIndicator];
    }
    return self;
}

I am instantiating INMenuCard with (debugging shows the CGRect values are correct):

 CGRect cardFrame = CGRectMake(cardX, cardStartY, cardWidth, cardHeight);
 INMenuCard *menuCard = [[INMenuCard buttonWithType:UIButtonTypeCustom] initWithFrame:cardFrame];
 [theView addSubView:menuCard];
+3  A: 

Should you be calling initWithFrame on something that is already init'ed?

It seems to me that the line [INMenuCard buttonWithType:UIButtonTypeCustom] is calling the UIButton's shortcut which does the [[UIButton alloc] init] for you.

ongle
My understanding is that the line [INMenuCard buttonWithType:UIButtonTypeCustom] is the "alloc" and then the initWithFrame is doing the "init-ing". I have verified that initWithFrame is being called correctly just not with the same frame value being supplied when the object is initiated.
Greypoint
I cannot find examples of people calling `[[UIButton buttonWithType:] init]` I am pretty sure that this class method returns an `init`'ed object. I also see a post (http://www.blogs.abeazam.com/dev/?p=77) that indicates that `buttonWithType` can't be used when extending `UIButton` because it returns a private class.
ongle
A HA! Thank you Ongle. Changing buttonWithType to just alloc solved that problem. Now I just have to figure out why my button's background image is hiding the activityindicator subview....but that's a new question. Thanks for the help!
Greypoint