ios 为什么自定义 UIButton 图像不会在 Interface Builder 中调整大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1063019/
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
Why does a custom UIButton image does not resize in Interface Builder?
提问by willc2
Why does a custom UIButton image not resize with the button?
为什么自定义 UIButton 图像不随按钮调整大小?
I set its view mode to Scale to Fillin Interface Builder, but unlike a UIImageView image, it doesn't respect that setting.
我将其视图模式设置为Scale to Fillin Interface Builder,但与 UIImageView 图像不同,它不遵守该设置。
Am I looking in the wrong place or is it not possible?
我找错了地方还是不可能?
回答by teabot
While this may not be quite what you're after - the background image doesscale.
虽然这可能不是您所追求的 - 背景图像确实可以缩放。
回答by Cao Huu Loc
Try this:
尝试这个:
[button setImage:image forState:UIControlStateNormal];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
In Swift:
在斯威夫特:
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
回答by JP Illanes
I know this is old, but I think that the correct answer is that you should set the content mode to the "hidden" UIImageView that is inside the UIButton:
我知道这是旧的,但我认为正确的答案是您应该将内容模式设置为 UIButton 内的“隐藏”UIImageView:
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
This will change the content mode for the images views of the images set with:
这将更改图像设置的图像视图的内容模式:
[button setImage:yourAwesomeImage forState:UIControlStateNormal];
//Or any other state
回答by Sandip Patel - SM
Just do (From Design OR From Code):
只是做(从设计或从代码):
From Design
从设计
- Open your xib OR Storyboard.
- Select button
- Inside Attribute Inspector(Right side) > In "Control"section > select last 4th optionfor both Horizontal and Verical.
- 打开您的 xib 或故事板。
- 选择按钮
- 属性检查器内部(右侧)> 在“控制”部分>为水平和垂直选择最后一个第四个选项。
[For Point#3: Change Horizontal and vertical Align to UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]
[对于第 3 点:将水平和垂直对齐更改为 UIControlContentHorizontalAlignmentFill 和 UIControlContentVericalAlignmentFill]
From Code
从代码
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
回答by George Filippakos
Interface Builder
界面生成器
To get images in UIButtons to scale the same way as UIViews in interface Builder follow these steps:
要使 UIButtons 中的图像以与界面 Builder 中的 UIViews 相同的方式缩放,请执行以下步骤:
Select your UIButton and from the Identity Inspectoradd the following to User Defined Runtime Attributes:
选择您的 UIButton 并从Identity Inspector中将以下内容添加到 User Defined Runtime Attributes:
imageView.contentMode Number 1
imageView.contentMode 编号 1
Where number is the enum number of contentMode, eg:
其中 number 是 contentMode 的枚举数,例如:
0 = Scale to fill
1 = Aspect Fit
2 = Aspect Fill
etc..
Then open the Attributeseditor for your UIButton and under Controlset the Alignmentto Fill (last button on right) for both horizontal and vertical.
然后打开UIButton的属性编辑器,并在Control下将水平和垂直的对齐设置为填充(右侧最后一个按钮)。
SWIFT 3
斯威夫特 3
Note you can also do this in code with the following:
请注意,您还可以使用以下代码在代码中执行此操作:
button.imageView?.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
回答by purebreadd
Updating this post will full answer for Swift 5:
更新这篇文章将为 Swift 5 提供完整答案:
//Set button's image
let image = UIImage(systemName: "heart.fill") //Your image here
button.setImage(image, for: .normal)
//Optional: Remove background image if you added one on accident
button.setBackgroundImage(nil, for: .normal)
//Optional: Set button's title (displayed next to button's image)
button.setTitle(nil, for: .normal)
//The button's "content" includes the title label and image
//So we set the button's content to fill the button
//If title label is nil, the content will only show the image
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
//Set button's image to desired content mode (aspectFit avoids distortion)
//This restricts the image from resizing to fill the entire content area
button.imageView?.contentMode = .scaleAspectFit
回答by Dshutsi
Try setting the Clip subviews property in Interface Builder to true.
尝试将 Interface Builder 中的 Clip subviews 属性设置为 true。
Hope that helps.
希望有帮助。
回答by Menno
The best solution I found so far in Swift 2.1 is the following:
到目前为止,我在 Swift 2.1 中找到的最佳解决方案如下:
self.imageButton.imageView?.clipsToBounds = true
self.imageButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
This will scale the image so it has an aspect fit in the imageView.
这将缩放图像,使其具有适合 imageView 的方面。
回答by Rene
Just get a real UIimageview and place a UIbutton on top of it and via layout menu set it to back. Then you can scale your image properly.
只需获取一个真正的 UIimageview 并在其顶部放置一个 UIbutton,然后通过布局菜单将其设置为背面。然后您可以正确缩放图像。