为什么 initWithFrame 的帧值错误?

发布于 2024-08-29 09:54:23 字数 924 浏览 2 评论 0原文

我有一个名为 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];

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-09-05 09:54:23

您是否应该对已经经过 init 处理的内容调用 initWithFrame

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文