为什么 initWithFrame 的帧值错误?
我有一个名为 INMenuCard 的 UIButton 子类,并且我正在重写 initWithFrame 以包含活动指示器。 menuCard 放置正确,但对“frame”的任何内部引用都会给我“inf,inf,0,0”,这意味着我的 ActivityIndicator 子视图放置不正确。我可能会缺少什么?
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否应该对已经经过
init
处理的内容调用initWithFrame
?在我看来,行
[INMenuCard buttonWithType:UIButtonTypeCustom]
正在调用UIButton
的快捷方式,该快捷方式执行[[UIButton alloc] init]
为你。Should you be calling
initWithFrame
on something that is alreadyinit
'ed?It seems to me that the line
[INMenuCard buttonWithType:UIButtonTypeCustom]
is calling theUIButton
's shortcut which does the[[UIButton alloc] init]
for you.