ios UIButton 不会转到 iPhone 中的 Aspect Fit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3460694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton won't go to Aspect Fit in iPhone
提问by marty
I have a couple UIButtons, and in IB they're set to Aspect Fit, but for some reason they're always stretching. Is there something else you have to set? I tried all the different view modes and none of them work, they all stretch.
我有几个 UIButton,在 IB 中它们被设置为 Aspect Fit,但由于某种原因,它们总是在拉伸。你还有什么需要设置的吗?我尝试了所有不同的视图模式,但都不起作用,它们都在拉伸。
采纳答案by Dan Ray
I've had that problem before. I solved it by putting my image in a UIImageView
, where contentMode
settings actually work, and putting a transparent custom UIButton
over top of that.
我以前也遇到过这个问题。我解决了这个问题,将我的图像放在一个UIImageView
,其中contentMode
设置实际起作用,并在其UIButton
之上放置一个透明的自定义。
EDIT: This answer is obsolete. See @Werner Altewischer's answerfor the correct answer in modern versions of iOS.
编辑:这个答案已经过时了。请参阅@Werner Altewischer 的答案,了解现代 iOS 版本中的正确答案。
回答by Werner Altewischer
The solution is to set the contentMode
on the imageView
property of the UIButton
. The UIButton
has to be created with custom type for this to work I believe (otherwise nil
is returned for this property).
解决方法是contentMode
在 的imageView
属性上设置UIButton
。在UIButton
必须与自定义类型(否则就创建了这个工作,我相信nil
返回该属性)。
回答by István Stefánovics
This method worked for me very well.:
这种方法对我非常有效。:
In Xibselect the button and set user defined runtime attributes
:
在Xib 中选择按钮并设置user defined runtime attributes
:
- Key path:
self.imageView.contentMode
- Type:
Number
- Value:
1
- 关键路径:
self.imageView.contentMode
- 类型:
Number
- 价值:
1
We use number because you cant use enum there. But the UIViewContentModeScaleAspectFit is equal to 1.
我们使用数字是因为你不能在那里使用枚举。但是 UIViewContentModeScaleAspectFit 等于 1。
回答by averydev
If you are doing this in Interface Builder, you can use the Runtime attributes inspector to set this directly without any code.
如果您在 Interface Builder 中执行此操作,则可以使用运行时属性检查器直接进行设置,无需任何代码。
Set your Key Path on the button to be "imageView.contentMode" with a type of "Number" and a value of "1" (or whichever mode you would like).
将按钮上的键路径设置为“imageView.contentMode”,类型为“数字”,值为“1”(或您喜欢的任何模式)。
回答by Ahmed Elashker
Use button's imageView for contentMode. Not directly on the button itself.
将按钮的 imageView 用于 contentMode。不是直接在按钮本身上。
homeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
[homeButton setImage:[UIImage imageNamed:kPNGLogo] forState:UIControlStateNormal];
回答by Alexander Wallin
If you put the image in an UIImageView
behind the button, you'll loose the built-in functionality of the UIButton
class, such as adjustsImageWhenHighlighted
and adjustsImageWhenDisabled
, and of course the ability to set different images for different states (without the hazzle of doing this yourself).
如果您将图像放在UIImageView
按钮后面,您将失去类的内置功能UIButton
,例如adjustsImageWhenHighlighted
and adjustsImageWhenDisabled
,当然还有为不同状态设置不同图像的能力(无需自己动手)。
If we want to have an image unstreched for all control states, one approuch is to get the image using imageWithCGImage:scale:orientation
, as in the following method:
如果我们想要为所有控件状态未拉伸图像,一种方法是使用 获取图像imageWithCGImage:scale:orientation
,如下面的方法:
- (UIImage *) getScaledImage:(UIImage *)img insideButton:(UIButton *)btn {
// Check which dimension (width or height) to pay respect to and
// calculate the scale factor
CGFloat imgRatio = img.size.width / img.size.height,
btnRatio = btn.frame.size.width / btn.frame.size.height,
scaleFactor = (imgRatio > btnRatio
? img.size.width / btn.frame.size.width
: img.size.height / btn.frame.size.height;
// Create image using scale factor
UIImage *scaledImg = [UIImage imageWithCGImage:[img CGImage]
scale:scaleFactor
orientation:UIImageOrientationUp];
return scaledImg;
}
To implement this we would write:
为了实现这一点,我们将编写:
UIImage *scaledImg = [self getScaledImage:myBtnImg insideButton:myBtn];
[myBtn setImage:scaledImg forState:UIControlStateNormal];
This should prevent the image from stretching in all control states. It worked for me, but let me know if it doesn't!
这应该可以防止图像在所有控制状态下拉伸。它对我有用,但如果没有,请告诉我!
NOTE:Here we are addressing a problem relating to UIButton
, but the insideButton:
might as well be insideView:
, or whatever one would like to fit the image into.
注意:这里我们正在解决一个与 相关的问题UIButton
,但insideButton:
也可能是insideView:
,或者任何人想要将图像放入其中的问题。
回答by Joe Barbour
I had this problem a while back. The issue I had was i was trying to apply this effect to the background UIButton which is limited and therefore means you cannot adjust it as easy.
不久前我遇到了这个问题。我遇到的问题是我试图将此效果应用到背景 UIButton 上,这是有限的,因此意味着您无法轻松调整它。
The trick is to set it as just an image then apply @ayreguitar's technique and that should fix it!
诀窍是将它设置为一个图像,然后应用@ayreguitar 的技术,这应该可以解决它!
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setContentMode:UIViewContentModeScaleAspectFill];
[myButton setImage:@"myImage.png" forState:UIControlStateNormal];
回答by Swindler
Combining together a few different answers into one solution-- create a button with a custom type, set the button's imageView contentMode property, and set the image for the button (not the background image, which will still scale to fill).
将几个不同的答案组合成一个解决方案——创建一个自定义类型的按钮,设置按钮的 imageView contentMode 属性,并设置按钮的图像(不是背景图像,它仍然会缩放以填充)。
//Objective-C:
UIImage *image = [UIImage imageNamed:"myImageName.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//Swift:
let image = UIImage(named: "myImageName.png")
let button = UIButton(type: .custom)
button.imageView?.contentMode = .scaleAspectFit
button.setImage(image, for: .normal)
回答by Karan Alangat
This worked for me
这对我有用
[button.imageView setContentMode:UIViewContentModeScaleAspectFit];
Thanks to @ayreguitar for his comment
感谢@ayreguitar 的评论
回答by Jo?o Neves
Here's an alternative answer in swift:
这是快速的替代答案:
myButton.imageView?.contentMode = .ScaleAspectFit