为什么initWithFrame的帧值错误?

时间:2010-04-17 15:11:09

标签: iphone uiview uibutton subclass

我有一个名为INMenuCard的UIButton子类,我正在重写initWithFrame以包含一个活动指示符。 menuCard正确放置,但任何对“frame”的内部引用都会给我“inf,inf,0,0”,这意味着我的activityIndi​​cator子视图未正确放置。我可能会缺少什么?

@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;
}

我正在实例化INMenuCard(调试显示CGRect值是正确的):

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

1 个答案:

答案 0 :(得分:3)

您是否应该对已经initWithFrame'的内容发送init

在我看来,[INMenuCard buttonWithType:UIButtonTypeCustom]行正在调用UIButton的快捷方式,为您执行[[UIButton alloc] init]