OgreSwamp
OgreSwamp

Reputation: 4692

.normal option doesn't work in state OptionSet for UIButton (Swift 3)

I'm trying to set same UIImage for 2 UIButton's states - Normal and Highlighted

UIControlState is an OptionSet, so it should work if I pass an array.

myButton.setImage(UIImage(named: myButtonImageName), for: [.normal, .highlighted])

But the code above set only .highlighted state and ignores .normal (image set previously in IB is still displayed in .normal state)

But, if I run it as 2 methods it works:

myButton.setImage(UIImage(named: myButtonImageName), for: .normal)
myButton.setImage(UIImage(named: myButtonImageName), for: .highlighted)

Am I missing something?

Upvotes: 1

Views: 434

Answers (1)

OOPer
OOPer

Reputation: 47886

In Swift, array literal for OptionSet represents a bitwise-ORed value.

And

UIControlState.normal.rawValue //->0
UIControlState.highlighted.rawValue //->1

Thus:

([.normal, .highlighted] as UIControlState).rawValue //->1

Taking an OptionSet type does not mean you can always pass a combined value to it. In your case your as 2 methods is needed.

Upvotes: 1

Related Questions