在写程序的时候想要给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的坐标。如果两个坐标写成一样的,那么就看不到添加的背景图片了。