UIButton setBackgroundImage和setImage方法的区别
1个回答
展开全部
区别如下:
1,setBackgroundImage,图片会随着按钮的大小而改变,图片自动会拉伸来适应按钮的大小,这个时候任然可以设置按钮的title,图片不会挡住title;
2,setImage,此时再设置title,title将无法显示。图片不会随着按钮的大小改变而改变。
代码说明下,使用的图片的大小是70*27:
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setBackgroundImage:[UIImage imageNamed:@"sign.png"] forState:UIControlStateNormal];
[btn2 setTitle:@"AAA" forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 75, 27 );
[self.view addSubview:btn2 ];1234512345
采用setImage:
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"sign.png"] forState:UIControlStateNormal];
[btn2 setTitle:@"AAA" forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 75, 27 );
[self.view addSubview:btn2 ];```
![setImage的看不到AAA的title](http://img.blog.csdn.net/20150710142744237)123456789123456789
把按钮的大小改变,代码如下:
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setBackgroundImage:[UIImage imageNamed:@”sign.png”] forState:UIControlStateNormal];
[btn2 setTitle:@”AAA” forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 175, 27 );
[self.view addSubview:btn2 ];
效果如下
采用setImage,代码如下
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@”sign.png”] forState:UIControlStateNormal];
[btn2 setTitle:@”AAA” forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 175, 27 );
[self.view addSubview:btn2 ];
“`
1,setBackgroundImage,图片会随着按钮的大小而改变,图片自动会拉伸来适应按钮的大小,这个时候任然可以设置按钮的title,图片不会挡住title;
2,setImage,此时再设置title,title将无法显示。图片不会随着按钮的大小改变而改变。
代码说明下,使用的图片的大小是70*27:
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setBackgroundImage:[UIImage imageNamed:@"sign.png"] forState:UIControlStateNormal];
[btn2 setTitle:@"AAA" forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 75, 27 );
[self.view addSubview:btn2 ];1234512345
采用setImage:
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"sign.png"] forState:UIControlStateNormal];
[btn2 setTitle:@"AAA" forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 75, 27 );
[self.view addSubview:btn2 ];```
![setImage的看不到AAA的title](http://img.blog.csdn.net/20150710142744237)123456789123456789
把按钮的大小改变,代码如下:
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setBackgroundImage:[UIImage imageNamed:@”sign.png”] forState:UIControlStateNormal];
[btn2 setTitle:@”AAA” forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 175, 27 );
[self.view addSubview:btn2 ];
效果如下
采用setImage,代码如下
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@”sign.png”] forState:UIControlStateNormal];
[btn2 setTitle:@”AAA” forState:UIControlStateNormal];
btn2.frame = CGRectMake( 10 , 200, 175, 27 );
[self.view addSubview:btn2 ];
“`
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询