2011-10-04 112 views
1

我在滚动视图内创建了一个自定义UIButton。 但图片不显示,有什么想法我做错了什么?iphone自定义UIButton不显示图片

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [button setBackgroundImage:[UIImage imageNamed:@"anzahl.png"] forState:UIControlStateNormal]; 
       [button setTitle:@"-" forState:UIControlStateNormal]; 
       button.frame = CGRectMake(x, y, viewWidth, viewHeight);//width and height should be same value 
       button.clipsToBounds = YES; 
       button.layer.cornerRadius = 20;//half of the width 
       button.layer.borderColor=[UIColor blueColor].CGColor; 
       button.layer.borderWidth=1.0f; 
       [button setAlpha:1.0]; 
       [scroller1 addSubview:button]; 
       [button release]; 
+0

你传递了​​正确的帧? – Hisenberg

+0

我自己对iOS有点新,但是如果它不是alloc/init-ed,你应该释放按钮吗? – Gregir

+0

伟大的:))这是问题,当我释放它没有显示。我首先用alloc创建了它,后来将其替换。谢谢问题解决 – Ferdi

回答

1

您不需要释放按钮,因为它没有被分配。你已经在UIButton上调用了一个类方法,所以不需要发布。

我还建议你先确认图像被正确加载,因此,测试

UIImage* backgroundImage = [UIImage imageNamed:@"anzahl.png"]; 
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal]; //breakpoint and ensure that backgroundImage is not nil (0x0) 

或者你可以有你的设置框坐标不正确,因此该按钮被绘制可视屏幕面积之外,所以下一步是确保您的viewWidth & viewHeight是正确的大小,而不是0.还要确保您的协调器位于屏幕的当前可见区域,而不是在可见屏幕边界之外(请记住, x,y位置将相对于它们所放置的超级视图(在本例中为scroller1))。

+0

是释放是我的问题谢谢你:) – Ferdi