iphone自定义UIButton不显示图片

发布于 2024-12-08 01:53:49 字数 784 浏览 0 评论 0原文

我在滚动视图中创建了一个自定义 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 技术交流群。

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

发布评论

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

评论(1

看轻我的陪伴 2024-12-15 01:53:49

您不需要释放该按钮,因为它尚未分配。您已经调用了 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 和 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

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

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)).

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