想要設(shè)置UIButton的Image大小最簡(jiǎn)單的一個(gè)方法
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15, 345, SCREEN_Width-30, 50)];
[button setImage:[UIImage imageNamed:@"scan"]?forState:UIControlStateNormal];
[button setTitle:@"掃碼添加" forState:UIControlStateNormal];
[button setTitleColor:mainColor forState:UIControlStateNormal];
[button setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:button];
設(shè)置UIButton 的 contentMode 可以讓圖片等比例縮放,設(shè)置 ?ImageEdgeInsets 可以縮小圖片嗦哆,必須兩個(gè)同時(shí)設(shè)置才能起效果燕鸽,讓一張?jiān)赽utton里面過大的圖片等比例縮小到你想要的大小.
樣圖