简体   繁体   English

如何在 UIButton 中设置图像大小?

[英]How to set image size in UIButton?

I'm trying to make a clone of decimal pad by stack views and UIButtons.我正在尝试通过堆栈视图和 UIButtons 制作小数点的克隆。 But when I try to make "Backspace" button image inside of it scales to the bounds of button.但是当我尝试在其中制作“Backspace”按钮图像时,它会缩放到按钮的边界。

How could I make image scale smaller than button itself?如何使图像比例小于按钮本身?

There may be two options to fix the problem.可能有两个选项可以解决问题。

  1. Set the content mode to .scaleAspectFit and the image should not go out of the bounds : myButton.imageView?.contentMode = .scaleAspectFit将内容模式设置为.scaleAspectFit并且图像不应越界: myButton.imageView?.contentMode = .scaleAspectFit

  2. Adjust the image insets: myLikesButton.imageEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right)调整图像插入: myLikesButton.imageEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right)

Sanket was close: Sanket 很近:

  1. Adjust the image insets: Button.imageEdgeInsets = UIEdgeInsetsMake(40, 40, 40, 40)调整图像插入:Button.imageEdgeInsets = UIEdgeInsetsMake(40, 40, 40, 40)

**The (40, 40, 40, 40) is just an example button sizing and you can adjust it to your satisfaction. ** (40, 40, 40, 40) 只是一个示例按钮大小,您可以将其调整到您满意的程度。 Like Sanket answered (Top, Left, Bottom, Right) is what you should use, just adjust with actual numbers.就像 Sanket 回答(上、左、下、右)是你应该使用的一样,只需根据实际数字进行调整。

Things you should keep in mind while loading image to the 'UIBUTTON'将图像加载到“UIBUTTON”时应该记住的事情

  • Try to use image size of 24*24 image when you are loading images to UIBARBUTTONIMAGE.将图像加载到 UIBARBUTTONIMAGE 时,尝试使用 24*24 图像大小。
  • When loading image in normal 'UIBUTTON' the image should be compatible with the button.在正常的“UIBUTTON”中加载图像时,图像应该与按钮兼容。
  • Try the button with standard image size which is 32*32.尝试使用标准图像尺寸 32*32 的按钮。
  • In the right interface builder in storyboard try with Aspect fit.在故事板中的正确界面构建器中尝试使用 Aspect fit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM