Ios UIButton框架不随图像调整大小
我创建了一个UIButton,使用图像作为按钮。但是,我无法调整图像/按钮的大小。我使用的代码是:Ios UIButton框架不随图像调整大小,ios,swift,uiimageview,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.cente
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(),这样它可以覆盖整个按钮框。尝试使用leftArmButton.setImage()代替leftArmButton.setBackgroundImage(),这样它可以覆盖整个按钮框。尝试此操作
leftArmButton.imageView?.contentMode = .scaleAspectFill
试试这个
leftArmButton.imageView?.contentMode = .scaleAspectFill
试试这个
leftArmButton.imageView?.contentMode = .scaleAspectFill
以编程方式
button.setImage(image, for: .normal)
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
情节提要
leftArmButton.imageView?.contentMode = .scaleAspectFill
以编程方式
button.setImage(image, for: .normal)
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
情节提要
您确定您的按钮尚未调整大小,但图像没有覆盖该区域吗?试着在你认为按钮不在的区域按下,它会触发动作吗?(假设您已将操作附加到按钮)编辑:您的图像设置为什么内容模式?请尝试使用
leftArmButton.setImage()
,而不是leftArmButton.setBackgroundImage()
,以便它可以覆盖整个按钮框。@Jerland2按钮正在调整大小,但图像没有。按钮区域正确触发操作。至于图像的内容模式,我尝试在所有三个选项中循环,但没有一个选项填满了框架。你确定你的按钮没有调整大小,但图像没有覆盖该区域吗?试着在你认为按钮不在的区域按下,它会触发动作吗?(假设您已将操作附加到按钮)编辑:您的图像设置为什么内容模式?请尝试使用leftArmButton.setImage()
,而不是leftArmButton.setBackgroundImage()
,以便它可以覆盖整个按钮框。@Jerland2按钮正在调整大小,但图像没有。按钮区域正确触发操作。至于图像的内容模式,我尝试了在所有三个选项中循环,但没有一个填充到框架中。它说UIContentMode没有成员“aspectFill”。对不起,我不在我的计算机上,但我更新了答案。如果它不起作用,只需键入一个
,然后等待xcode弹出选项,自动将其填充到AspectFill中。没问题。这是一个选项,但它没有调整图像的大小。它说UIContentMode没有成员“aspectFill”。很抱歉,我不在我的计算机上,但我更新了答案。如果它不起作用,只需键入一个
,然后等待xcode弹出选项,自动将其填充到AspectFill中。没问题。这是一个选项,但它没有调整图像大小这似乎仍然没有填充按钮框。我使框架可见,以便我可以验证这似乎仍然没有填充按钮框架。我使框架可见,以便验证这一点