iOS - UIButton中调整image大小,使其缩放

想要设置UIButton的Image大小最简单的一个方法


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];

设置UIButton 的 contentMode 可以让图片等比例缩放,设置  ImageEdgeInsets 可以缩小图片,必须两个同时设置才能起效果,让一张在button里面过大的图片等比例缩小到你想要的大小.


样图

你可能感兴趣的:(iOS - UIButton中调整image大小,使其缩放)