iphone自定义UIButton不显示图片
我在滚动视图中创建了一个自定义 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];
i created a custom UIButton inside a scrollview.
But the picture does not show, any idea what i am doing wrong ?
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要释放该按钮,因为它尚未分配。您已经调用了 UIButton 上的类方法,因此不需要释放。
我还建议您首先验证图像是否正确加载,以便测试
或者您可能将框架坐标设置不正确,以便按钮在可见屏幕区域之外绘制,因此下一步是确保您的 viewWidth & ; viewHeight 的大小正确,而不是 0。还要确保您的 x 和 viewHeight 的大小正确。 y 坐标位于屏幕的当前可见区域中,并且不在可见屏幕边界之外(请记住,x,y 位置将相对于它们所在的超级视图(在本例中为 roller1))。
You don't need to release the button, as it's not been allocated. You've called a class method on UIButton, so no release is needed.
I'd also suggest you first verify that the image is loading correctly so test
Or you could have your frame co-ordinates set incorrectly so that the button is drawing outside of the visible screen area, so the next step is ensure that your viewWidth & viewHeight are the correct size and not 0. Also make sure that your x & y co-ordiantes are in the currently visible area of the screen and not outside the visible screen bounds (remember that the x,y position will be relative to the super view they are placed in (in this case scroller1)).