在Simulator中选择时,UIButton中的UIImageView无法正确呈现

时间:2021-07-16 00:23:59

This is how it looks in storyboard:

这就是它在故事板中的样子:

在Simulator中选择时,UIButton中的UIImageView无法正确呈现

... and after selected:

......之后选择:

在Simulator中选择时,UIButton中的UIImageView无法正确呈现

This is my subclass:

这是我的子类:

@IBDesignable class WLButton: UIButton {

    override var selected: Bool {

        didSet {

            backgroundColor = selected ? UIColor.scooter() : UIColor.clearColor()
            imageView?.tintColor = selected ? UIColor.whiteColor() : UIColor.grayColor()
            imageView?.image = imageView?.image?.imageWithRenderingMode(.AlwaysTemplate)
        }
    }
}

And this is output in Simulator when selected:

这在选择时在模拟器中输出:

在Simulator中选择时,UIButton中的UIImageView无法正确呈现

Why imageView is not rendered as it should?

为什么imageView没有按照它应该渲染?

1 个解决方案

#1


0  

Change last 2 lines to:

将最后2行更改为:

setImage(imageView?.image?.imageWithRenderingMode(.AlwaysTemplate), forState: UIControlState.Selected)
tintColor = UIColor.whiteColor()

#1


0  

Change last 2 lines to:

将最后2行更改为:

setImage(imageView?.image?.imageWithRenderingMode(.AlwaysTemplate), forState: UIControlState.Selected)
tintColor = UIColor.whiteColor()