我创建名为“键”一个UIButton实例与使用图像[UIButton setImage:forState:]
。 该button.frame大于图像的大小。
现在我想扩大这个按钮的图像小。 我试图改变button.imageView.frame
, button.imageView.bounds
和button.imageView.contentMode
,但都显得无效。
谁能帮我缩放UIButton
的ImageView的?
我创建UIButton
是这样的:
UIButton *button = [[UIButton alloc] init];
[button setImage:image forState:UIControlStateNormal];
我试着缩放图像是这样的:
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.bounds = CGRectMake(0, 0, 70, 70);
还有这个:
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.frame = CGRectMake(0, 0, 70, 70);
Answer 1:
对于楼主,这里是我找到了解决办法:
commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
这将使你的按钮水平缩放。 有一个垂直设置为好。
我花了几个小时才能搞懂这个问题(该属性的命名是非常直观),所以想我会分享。
Answer 2:
我会遇到类似的问题,在这里我已经为一个自定义按钮的背景图像(一个有边框)和图像(标志)。 我想标志被缩小和中心。 我试图改变的ImageView属性,但没有成功,并看到了这个形象 -
在我的实验,我想:
button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)
我取得了预期的结果:
Answer 3:
加法的方式在XIB文件中的配置。 如果你想要的文字或图片中的UIButton规模完全填补你可以选择此选项。 这将与代码相同的。
UIButton *btn = [UIButton new];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Answer 4:
使用
button.contentMode = UIViewContentModeScaleToFill;
不
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
更新:
由于@克里斯评论,
要获得此为setImage工作:forState:,你需要做以下水平缩放:myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
Answer 5:
UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,70)];
button.buttonType = UIButtonTypeCustom;
UIImage *buttonImage = [UIImage imageNamed:@"image.png"];
UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];
Answer 6:
我发现这个解决方案。
1)子类的下列方法UIButton
+ (id)buttonWithType:(UIButtonType)buttonType {
MyButton *toReturn = [super buttonWithType:buttonType];
toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit;
return toReturn;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
return contentRect;
}
而且效果很好。
Answer 7:
奇怪,只为我工作的组合(5.1 iOS版)是...
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
和
[button setImage:newImage forState:UIControlStateNormal];
Answer 8:
我只是碰到了这个同样的问题,有一个在这个问题上可能的答案:
为什么一个自定义的UIButton图像不会在Interface Builder调整?
从本质上讲,使用BackgroundImage属性代替,这也得到扩展。
Answer 9:
只要做到(从设计还是从代码):
从设计
- 打开厦门国际银行或故事板。
- 选择按钮
- 内部属性检查器 (右侧)>在“控制”部分>选择水平和中古立式去年第四选项 。
[对于点#3:改变水平和垂直对齐到UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]
从代码
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Answer 10:
这样可以解决你的问题:
+ (UIImage*)resizedImage:(UIImage*)image
{
CGRect frame = CGRectMake(0, 0, 60, 60);
UIGraphicsBeginImageContext(frame.size);
[image drawInRect:frame];
UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
Answer 11:
从小的测试,我只是阅读后在这里做,取决于你使用setImage或了setBackgroundImage,既做了同样的结果,并STRETCH时的图像
//for setBackgroundImage
self.imageButton.contentMode = UIViewContentModeScaleAspectFill;
[self.imageButton setBackgroundImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];
//for setImage
self.imageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.imageButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
[self.imageButton setImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];
Answer 12:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Answer 13:
我不能_imageView得到一个解决方案的使用,但我可以用一个CGContextRef
来解决它。 它使用UIGraphicsGetCurrentContext
得到currentContextRef和currentContextRef绘制图像,然后缩放或旋转图像,并创建一个新的形象。 但它并不是完美的。
代码:
-(UIImage*) scaleAndRotateImage:(UIImage*)photoimage width:(CGFloat)bounds_width height:(CGFloat)bounds_height;
{
CGImageRef imgRef = photoimage.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
bounds.size.width = bounds_width;
bounds.size.height = bounds_height;
CGFloat scaleRatio = bounds.size.width / width;
CGFloat scaleRatioheight = bounds.size.height / height;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = photoimage.imageOrientation;
switch(orient)
{
case UIImageOrientationUp: //EXIF = 1
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight: //EXIF = 8
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"];
break;
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
{
CGContextScaleCTM(context, -scaleRatio, scaleRatioheight);
CGContextTranslateCTM(context, -height, 0);
}
else
{
CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}
Answer 14:
这也将工作,因为和backgroundImage自动缩放
[button setBackgroundImage:image forState:UIControlStateNormal];
Answer 15:
故事板
-必须在身份检查的运行属性定义特定的属性imageView.contentModel
,你设置的值相对于rawValue
枚举的位置UIViewContentMode
。 1表示scaleAspectFit。
和按钮的排列,在属性检查器 :
Answer 16:
在左下方的截图,你可以看到拉伸性能。 尝试使用这些。
Answer 17:
我希望这可以帮助别人:
斯威夫特3+
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.fill
button.contentVerticalAlignment = UIControlContentVerticalAlignment.fill
Answer 18:
每一个的UIButton都有自己的一个隐藏的UIImageView。 所以我们要设置内容模式类似如下的方式......
[[btn imageView] setContentMode: UIViewContentModeScaleAspectFit];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
文章来源: How do I scale a UIButton's imageView?