ios 如何不在 UIButton 中拉伸图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11346697/
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
How to not stretch an image in UIButton
提问by Nosrettap
I'm trying to create a custom UITableViewCell programmatically and one of the subviews of this cell is going to be a button with an image in it (a simple image of a magnifying glass). However, I want the button's image to be centered and scaled proportionately down to fit in the button and NOT to be stretched to fill the entire button. Below is my code where self refers to the custom UITableViewCell which I am placing the button into.
我正在尝试以编程方式创建自定义 UITableViewCell 并且该单元格的子视图之一将是一个带有图像的按钮(放大镜的简单图像)。但是,我希望按钮的图像居中并按比例缩小以适合按钮,而不是拉伸以填充整个按钮。下面是我的代码,其中 self 指的是我将按钮放入的自定义 UITableViewCell 。
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton setBackgroundImage:[UIImage imageNamed: @image_name_here"] forState:UIControlStateNormal];
self.myButton.frame = CGRectMake(...//something, something)
self.myButton.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.mySearchHelpButton];
Right now the image stretches to fill the entire button rather than scaling proportionately so that it fits nicely.
现在,图像会拉伸以填充整个按钮,而不是按比例缩放以使其非常适合。
I have also tried setting the contentMode to UIViewContentModeScaleAspectFill
but this doesn't seem to change anything. In fact, none of the different contentModes seem to change anything.
我也试过将 contentMode 设置为,UIViewContentModeScaleAspectFill
但这似乎没有改变任何东西。事实上,不同的 contentModes 似乎都没有改变任何东西。
回答by Robot Woods
Have you tried setImage
instead of setBackgroundImage
?
你试过setImage
代替setBackgroundImage
吗?
回答by Dimitris
Make sure the button is a Custom UIButton
确保按钮是自定义 UIButton
Just using setImage:
did not work for me on iOS9.
setImage:
在 iOS9 上仅使用对我不起作用。
But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;
但它与 myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;
回答by Blank
In Swift...
在斯威夫特...
button.imageView?.contentMode = . scaleAspectFit
回答by dimpiax
Storyboard
故事板
Propertyimage.
财产形象。
You must define specific property in Runtime Attributesof Identity inspector– imageView.contentMode
, where you set value relatively to rawValue
position of enum UIViewContentMode
. 1 means scaleAspectFit.
您必须在身份检查器的运行时属性中定义特定属性– ,您可以在其中设置相对于enum 位置的值。1 表示scaleAspectFit。imageView.contentMode
rawValue
UIViewContentMode
And button's alignment, in Attributes inspector:
和按钮的对齐方式,在属性检查器中:
回答by Abhishek Jain
Swift 4.2
斯威夫特 4.2
myButton.imageView!.contentMode = .scaleAspectFit
Swift earlier version
Swift 早期版本
myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit
回答by J. Doe
Swift 4.X and 5.X
斯威夫特 4.X 和 5.X
button.imageView!.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
回答by Sherwin Zadeh
I think you guys are missing the obvious issue. The image is too large for the button and iOS then has to guess how you want to scale it. Make sure your images are the right size and you won't have this issue. Of course this is for static images and buttons that you know the size for up-front -- not dynamic content.
我认为你们错过了明显的问题。图像对于按钮来说太大了,然后 iOS 必须猜测您想要如何缩放它。确保您的图像大小正确,您就不会遇到此问题。当然,这适用于您预先知道大小的静态图像和按钮——而不是动态内容。