不遵守UIButton的backgroundImage的内容模式 [英] UIButton's backgroundImage's content mode not respected
问题描述
我要在要发布的应用程序上进行最后的修饰,并且遇到了将 UIButton
的背景图像的 contentMode
移至被尊重.不管我指定了什么 contentMode
,图像都只会在后台放映,而与 contentMode
设置无关.
I'm putting the finishing touches on an app I'm about to publish and I've enountered difficulty getting the contentMode
of a UIButton
's background image to be respected. Regardless of the contentMode
I specify, the image just gets plopped in the background with no regard for the contentMode
setting.
我看过一篇有关该主题的帖子,但是在我看来,它似乎没有任何作用
I've looked at a post covering this topic, but it doesn't appear to do anything in my circumstance.
这是从 viewDidLoad
调用的:
// I tried putting the contentMode lines here...
myButton.imageView!.contentMode = .scaleAspectFit
myButton.contentMode = .scaleAspectFit
myButton.setBackgroundImage(UIImage(named: "myImage.png"), for: .normal)
// ...and here
// myButton.imageView!.contentMode = .scaleAspectFit
// myButton.contentMode = .scaleAspectFit
// I threw this in for S's & G's to see if it would work...it didn't
myButton.translatesAutoresizingMaskIntoConstraints = false
myButton.imageView?.translatesAutoresizingMaskIntoConstraints = true
我不确定自己缺少什么,但是我可以放心,这对我来说将是一件令人震惊的愚蠢的事情.我欢迎提出建议,让我了解如何获得 UIButton
的背景图片的 contentMode
.谢谢您的阅读.
I'm not sure what I'm missing, but I rest assured it will be something startling stupid on my part. I welcome suggestions on how I can get the contentMode
of a UIButton
's background image to be respected. Thank you for reading.
更新
该按钮被键入为自定义按钮,而不是系统按钮.
The button is typed as a custom button, not a system button.
推荐答案
问题是您使用 setBackgroundImage()
方法添加了图像.这会将您的图像添加到一些像 myButton.imageView!
这样无法直接访问的私有 UIImageView
中.在您的代码中,您正在为按钮应用 contentType
而不是其背景图片.
The problem is that you add an image using setBackgroundImage()
method. This adds your image to some private UIImageView
that you can't reach directly like myButton.imageView!
. In your code, you're applying contentType
for you button instead of its background image.
如果要达到此背景 UIImageView
,则需要使用 subviews
数组.但是,如果尝试在 viewDidLoad()
方法中执行此操作,则会发现尚未添加背景 UIImageView
,因为您的按钮 layout 没有调用code>方法.有两种方法可以解决此问题.
If you want to reach this background UIImageView
you need to use subviews
array. But if you try to do this inside your viewDidLoad()
method, you'll find that background UIImageView
hasn't been added yet because your buttons layout
method wasn't called. There are 2 ways to fix that.
解决方案1 –明确调用 layoutIfNeeded
:
override func viewDidLoad() {
super.viewDidLoad()
myButton.setBackgroundImage(UIImage(named: "myImage"), for: .normal)
myButton.layoutIfNeeded()
myButton.subviews.first?.contentMode = .scaleAspectFit
}
解决方案2 –使用 viewDidAppear
方法移动 contentMode
设置.
Solution 2 – move contentMode
setup at viewDidAppear
method.
override func viewDidLoad() {
super.viewDidLoad()
myButton.setBackgroundImage(UIImage(named: "myImage"), for: .normal)
myButton.setTitle("Some title", for: .normal)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
myButton.subviews.first?.contentMode = .scaleAspectFit
}
希望它对您有帮助.
这篇关于不遵守UIButton的backgroundImage的内容模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!