【发布时间】:2017-01-12 18:02:58
【问题描述】:
我创建了一个 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)
如何调整图片大小?
【问题讨论】:
-
您确定您的按钮尚未调整大小,但图像并未覆盖该区域吗?尝试在您认为没有按钮的区域按下,它会触发动作吗? (假设您已将操作附加到按钮)编辑:您的图像设置为哪种内容模式?
-
不要使用
leftArmButton.setImage(),尝试使用leftArmButton.setBackgroundImage(),这样它就可以覆盖整个按钮框架。 -
@Jerland2 按钮正在调整大小,但图像没有。按钮区域正确触发动作。至于图像的内容模式,我尝试循环浏览所有三个选项,但都没有填满框架。
标签: ios swift uiimageview uibutton