【问题标题】:UIButton ImageView contentmode doesn't workUIButton ImageView 内容模式不起作用
【发布时间】:2019-05-24 09:36:36
【问题描述】:

我刚刚在 Swift 中为 iOS 启动了一个应用程序,我正在以编程方式进行设计(我的意思是我没有使用故事板)

我有一个带有图像的 UIButton,我希望图像填充整个按钮。现在它只在我的按钮中显示图像,但比按钮小,在按钮框架的顶部居中。

我的代码是:

    let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize))
    buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
    buttonLocation.backgroundColor = .white
    buttonLocation.layer.cornerRadius = frame.width / 2
    buttonLocation.myExtension()

这是我尝试过的东西(这些都不起作用):

    self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    self.autoresizingMask = .flexibleWidth
    self.autoresizingMask = .flexibleHeight
    self.imageView?.contentMode = .scaleAspectFill

如果我只是放置 contentMode 行,它也不起作用。我检查过的 imageView 没有返回 nil ..

谢谢!

【问题讨论】:

  • 试试 buttonLocation.contentMode = .scaleAspectFit
  • 使用buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)
  • @BenRockey 它不起作用
  • 尝试在按钮内部设置imageView对象的内容模式。前任。 buttonLocation.imageView?.contentMode = .scaleAspectFill
  • @BhargavR 也不起作用 :( 而且我也尝试过 setBackgroundImage 并且我遇到了完全相同的问题

标签: ios swift uibutton contentmode


【解决方案1】:

contentModebuttonLocation's imageView 设置为scaleAspectFillscaleToFill 以覆盖整个framebutton

let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 500))
buttonLocation.setImage(UIImage(named: "image"), for: .normal)
buttonLocation.backgroundColor = .gray
buttonLocation.layer.cornerRadius = buttonLocation.frame.width / 2
buttonLocation.imageView?.contentMode = .scaleAspectFill //Here........

示例:

【讨论】:

  • 正如您在我的第一篇文章中看到的那样,这正是我正在做的事情,但它不起作用
【解决方案2】:

尝试使用以下代码并删除为此按钮设置的其他属性:

let buttonLocation = UIButton(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
        buttonLocation.setImage(UIImage(named: "cat"), for: .normal)
        buttonLocation.backgroundColor = .white
        buttonLocation.layer.cornerRadius = 50
        buttonLocation.imageView?.contentMode = .scaleAspectFill
        buttonLocation.clipsToBounds = true

        self.view.addSubview(buttonLocation)

这里确保您在按钮中设置的图像大于按钮的框架。您可以在上面的代码中更改框架和角半径值。我刚刚尝试使用上面的代码并得到以下输出。

如果这不是您的预期输出,您可以发表评论。

希望这会有所帮助。

【讨论】:

  • 还是不行,而且我的图片尺寸比 unbutton 尺寸大,所以我真的不明白为什么它不起作用...
【解决方案3】:

回答你的问题:https://stackoverflow.com/a/26263623/3047318

TLDR:在viewDidLoad 中设置 contentMode

【讨论】:

    【解决方案4】:

    您需要使用 setBackgroundImage 属性而不是 setImage。

    buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal)
    

    【讨论】:

    • 用 setBackgroundImage 代替 setImage 也有同样的问题
    【解决方案5】:

    也许这可以帮助你:

    let buttonLocation = UIButton(type: .custom)
    buttonLocation.frame = CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize)
    buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
    buttonLocation.imageView?.contentMode = .scaleAspectFill
    buttonLocation.contentHorizontalAlignment = .fill
    buttonLocation.contentVerticalAlignment = .fill
    buttonLocation.backgroundColor = .white
    buttonLocation.layer.cornerRadius = frame.width / 2
    buttonLocation.clipsToBounds = true
    

    您需要添加contentHorizontalAlignmentcontentVerticalAlignment 属性,并且不要忘记将clipsToBounds 设置为true 以查看角落raius 效果。

    【讨论】:

    • 同样的问题
    • myExtension() 里面叫什么。尝试删除imageEdgeInsets 并重新测试。
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多