【问题标题】:iphone Custom UIButton does not show pictureiphone自定义UIButton不显示图片
【发布时间】:2011-10-04 15:45:49
【问题描述】:

我在滚动视图中创建了一个自定义 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];

【问题讨论】:

  • 你传递的帧是否正确??
  • 我自己对 iOS 有点陌生,但如果它不是分配/初始化的,你应该释放按钮吗?
  • 太棒了 :)) 这就是问题所在,当我释放它时它没有显示出来。我首先使用带有框架的 alloc 创建它......后来替换了它。谢谢问题解决了

标签: iphone image uibutton scrollview


【解决方案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 坐标位于屏幕的当前可见区域,而不是可见屏幕边界之外(请记住,x、y 位置将相对于它们所在的超级视图(在本例中为 scroller1))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2020-06-29
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2015-01-07
    相关资源
    最近更新 更多