vcL*_*wei 79 scale uibutton ios
我用图像创建了一个名为"button"的UIButton实例[UIButton setImage:forState:].button.frame大于图像的大小.
现在我想缩小此按钮的图像.我试图改变button.imageView.frame,button.imageView.bounds和button.imageView.contentMode,但都显得无效.
任何人都可以帮助我缩放图像UIButton视图吗?
我创建了UIButton这样的:
UIButton *button = [[UIButton alloc] init];
[button setImage:image forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
我试图像这样缩放图像:
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.bounds = CGRectMake(0, 0, 70, 70);
Run Code Online (Sandbox Code Playgroud)
还有这个:
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.frame = CGRectMake(0, 0, 70, 70);
Run Code Online (Sandbox Code Playgroud)
Chr*_*ris 87
对于原始海报,这是我找到的解决方案:
commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)
这将允许您的按钮水平缩放.还有垂直设置.
花了我几个小时来计算出那一个(财产的命名非常不直观)所以我想分享.
Hit*_*iya 63
我遇到了类似的问题,我有一个背景图像(一个带边框)和一个自定义按钮的图像(标志).我希望旗帜缩小并放在中心位置.我尝试更改imageView的属性,但没有成功,并看到这个图像 -

在我的实验中,我尝试过:
button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)
Run Code Online (Sandbox Code Playgroud)
我达到了预期的结果:

Lin*_*yen 35
XIB文件中配置的附加方式.如果您希望文本或图像是UIButton中的Scale full fill,则可以选择此选项.它与代码相同.
UIButton *btn = [UIButton new];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)
小智 22
使用
button.contentMode = UIViewContentModeScaleToFill;
Run Code Online (Sandbox Code Playgroud)
不
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
Run Code Online (Sandbox Code Playgroud)
更新:
正如@Chris评论的那样,
要使其适用于setImage:forState:,您需要执行以下操作以水平扩展:myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
EEE*_*EEE 15
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];
Run Code Online (Sandbox Code Playgroud)
mar*_*cio 10
我找到了这个解决方案
1)子类以下方法 UIButton
+ (id)buttonWithType:(UIButtonType)buttonType {
MyButton *toReturn = [super buttonWithType:buttonType];
toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit;
return toReturn;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
return contentRect;
}
Run Code Online (Sandbox Code Playgroud)
它运作良好.
奇怪,唯一适合我的组合(iOS 5.1)是......
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
和
[button setImage:newImage forState:UIControlStateNormal];
只做(来自设计或来自代码):
[对于点#3:更改水平和垂直对齐 UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)
小智 5
这样可以解决你的问题:
+ (UIImage*)resizedImage:(UIImage*)image
{
CGRect frame = CGRectMake(0, 0, 60, 60);
UIGraphicsBeginImageContext(frame.size);
[image drawInRect:frame];
UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
Run Code Online (Sandbox Code Playgroud)
从我在这里阅读后做的小测试,取决于你是否使用setImage或setBackgroundImage,两者都做了相同的结果并拉伸图像
//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];
Run Code Online (Sandbox Code Playgroud)
您必须在身份检查器\xe2\x80\x93的运行时属性中定义特定属性,在其中相对于enum 的位置设置值。1 表示scaleAspectFit。imageView.contentModelrawValueUIViewContentMode
以及按钮的对齐方式,在属性检查器中:
\n\n| 归档时间: |
|
| 查看次数: |
88987 次 |
| 最近记录: |