現在想定義一個UIButton, 這個button有兩張圖片, 一張算是背景圖, 另外一張顯示圖片1.
我通過setBackgroundImage設置了背景圖片, 用setImage 來設置顯示圖片 但是因為圖片 大於了我設置的按鈕的大小, 所以顯示出來的效果是圖片 超出了按鈕的框架.....
第一個按鈕是兩張圖片重疊的時候, 後面兩個是分別單獨加載的時候...
源代碼是這樣的:www.2cto.com
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame: CGRectMake(10, 10, 50, 50)];
[btn1 setBackgroundImage:[UIImage imageNamed: @"ui4.png"] forState:UIControlStateNormal];
[btn1 setImage:[UIImage imageNamed:@"icon0.png"] forState:UIControlStateNormal];
btn1.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview: btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn2 setFrame: CGRectMake(65, 10, 50, 50)];
[btn2 setBackgroundImage:[UIImage imageNamed: @"ui4.png"] forState:UIControlStateNormal];
btn2.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview: btn2];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn3 setFrame: CGRectMake(120, 10, 50, 50)];
btn2.contentMode = UIViewContentModeCenter;
[btn3 setBackgroundImage:[UIImage imageNamed:@"icon0.png"] forState:UIControlStateNormal];
[self addSubview: btn3];