Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton框架不随图像调整大小_Ios_Swift_Uiimageview_Uibutton - Fatal编程技术网

Ios 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

我创建了一个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(),这样它可以覆盖整个按钮框。

尝试使用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中。没问题。这是一个选项,但它没有调整图像大小这似乎仍然没有填充按钮框。我使框架可见,以便我可以验证这似乎仍然没有填充按钮框架。我使框架可见,以便验证这一点