UIButton 中的 AspectFit 图像 [英] AspectFit image inside an UIButton

查看:30
本文介绍了UIButton 中的 AspectFit 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的图像

I have an image looking like this

但它应该是这样的:

But it should look like this :

我添加了以下行:cameraUIButton.imageView?.contentMode = .scaleAspectFit

它不起作用但是如果我点击它适合的按钮!!当您单击重新计算图像适合度的按钮时,会在 swift 中附加什么?

我已经试过了

It doesn't works BUT if i click on the button it fit !! What append in swift when you click on the button that recalculate the fit of the image ?

I already tried

        cameraUIButton.setNeedsDisplay()
        cameraUIButton.imageView?.setNeedsDisplay()
        cameraUIButton.imageView?.layoutIfNeeded()
        cameraUIButton.layoutIfNeeded()
        self.view.layoutIfNeeded()

以及全部

viewDidLoad()
viewWillAppear()
viewDidAppear()

点击按钮没有任何作用,我还能尝试什么?

Nothing do what cliking on the button do, what else could i try ?

推荐答案

我找到了解决方案!!
您需要添加

cameraUIButton.imageView?.adjustsImageSizeForAccessibilityContentSizeCategory = true

所以在按钮内进行aspectFit矢量图像的完整代码是:

So the full code to aspectFit a vector image inside a button is :

cameraUIButton.imageView?.contentMode = .scaleAspectFit
cameraUIButton.imageView?.adjustsImageSizeForAccessibilityContentSizeCategory = true

我不知道为什么它不是 true 默认情况下,因为如果您单击按钮或动画它会转向它.

I don't know why it's not trueby default as it's turned to it if you click on the button or if you animate it.

这篇关于UIButton 中的 AspectFit 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆