我创建了一个UIButton,使用图像作为按钮。但是,我无法调整图像/按钮的大小。我正在使用的代码是:UIButton Frame Not Resizing with Image
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)
如何调整图像大小?
你确定你的按钮还没有调整大小,但图像不包括该区域?尝试按下你认为按钮不在的区域,是否触发一个动作? (假设您已将操作附加到按钮上)编辑:您的图像设置为什么内容模式? – Jerland2
尝试使用'leftArmButton.setBackgroundImage()'来代替'leftArmButton.setImage()',以便它可以覆盖整个按钮框架。 –
@ Jerland2该按钮正在调整大小,但图像不是。按钮区域正确触发动作。至于图片的内容模式,我尝试了三种选择的循环,但没有一个填满框架。 – DMop