我创建了一个 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
打开你的 Storyboard
在属性检查器(右侧)中选择按钮 -> 在“控制”部分 -> 为水平和垂直对齐选择第四个选项(最后一个)。
关于ios - UIButton 框架不随图像调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620005/