ios - UIButton 框架不随图像调整大小

标签 ios swift uiimageview uibutton

我创建了一个 UIButton,使用图像作为按钮。但是,我无法调整图像/按钮的大小。我使用的代码是:

let leftArmImage = UIImage(named: "LeftArm") as UIImage?
let leftArmButton   = UIButton(type: UIButtonType.custom) as UIButton
leftArmButton.frame = CGRect(x:self.view.center.x + 200,y:self.view.center.y,width:500,height:500)
leftArmButton.setImage(leftArmImage, for: .normal)
leftArmButton.imageView?.frame = leftArmButton.frame
leftArmButton.addTarget(self, action: #selector(PhysicalExam.bodyPartSingleTapped(_:)), for:.touchUpInside)
self.view.addSubview(leftArmButton)

如何调整图片大小?

最佳答案

试试这个

以编程方式

button.setImage(image, for: .normal)
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill

Storyboard

  1. 打开你的 Storyboard

  2. 在属性检查器(右侧)中选择按钮 -> 在“控制”部分 -> 为水平和垂直对齐选择第四个选项(最后一个)。

关于ios - UIButton 框架不随图像调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620005/

相关文章:

ios - 快速导入一个类未被识别

ios - 不同类型的 Controller 如何连接?

Swift for var list.enumerated()

swift - 用户可以从您的 Xcode 项目访问哪些文件/数据 - Swift 4

objective-c - 如何以 3d 圆形方式旋转 UIImageView?

swift - UIImageView 捏缩放 swift

ios - 有谁知道我如何找到 IOS5 Iphones/ipod 的 header 。越狱开发

swift - 为什么变量赋值之外的数字会编译?

ios - 如何将图像放在 Swift 的 UIImageView 上?

ios - UITableView 错误的部分标题