iOS OC uibutton添加背景图片后看不到

在写程序的时候想要给uibutton添加背景图片,结果试来试去总是不成功,最后才发现代码的错误。写下来以免再犯。

添加背景图片使用的是UIImage和UIImageView,出错就错在对UIImageView的初始化上。

    UIButton * counting_Days = [UIButton buttonWithType:UIButtonTypeCustom];
    [counting_Days setFrame:CGRectMake(145, 295, 40, 75)];
    UIImage *counting_Days_pic = [UIImage imageNamed:@"counting_Day.png"];
    UIImageView * counting_Days_imaview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 38, 75)];
    [counting_Days_imaview setImage:counting_Days_pic];
    [counting_Days addSubview:counting_Days_imaview];
    counting_Days.backgroundColor = [UIColor clearColor];
    [counting_Days addTarget:self action:@selector(counting_Days_Jump) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:counting_Days];

UIButton在初始化的时候的坐标就是UIButton的坐标,而UIImageView在初始化的时候的坐标则是它相对于UIButton的坐标。如果两个坐标写成一样的,那么就看不到添加的背景图片了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值