ios开发之UIButton 点击两张图标转换
现在想定义一个UIButton, 这个button有两张图片, 一张算是背景图, 另外一张显示图片1. 我通过setBackgroundImage设置了背景图片, 用setImage 来设置显示图片1. 但是因为图片1大于了我设置的按钮的大小, 所以显示出来的效果是图片1超出了按钮的框架..... 不知道该怎么弄了?! 想看看大家是怎么弄的?!
情况就像下面的图片一样: 第一个按钮是两张图片重叠的时候, 后面两个是分别单独加载的时候...毕业论文 源代码是这样的:
01.UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
02. [btn1 setFrame: CGRectMake(10, 10, 50, 50)];
03. [btn1 setBackgroundImage:[UIImage imageNamed: @"ui4.png"] forState:UIControlStateNormal];
04. [btn1 setImage:[UIImage imageNamed:@"icon0.png"] forState:UIControlStateNormal];
05. btn1.contentMode = UIViewContentModeScaleAspectFill;
06. [self addSubview: btn1];
07.
08. UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
09. [btn2 setFrame: CGRectMake(65, 10, 50, 50)];
10. [btn2 setBackgroundImage:[UIImage imageNamed: @"ui4.png"] forState:UIControlStateNormal];
11. btn2.contentMode = UIViewContentModeScaleAspectFill;
12. [self addSubview: btn2];
13.
14. UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
15. [btn3 setFrame: CGRectMake(120, 10, 50, 50)];
16. btn2.contentMode = UIViewContentModeCenter;
17. [btn3 setBackgroundImage:[UIImage imageNamed:@"icon0.png"] forState:UIControlStateNormal];
18. [self addSubview: btn3];
|