I was trying to make a simple tic-tac-toe app in Swift, so I set up 9 buttons with tags from 1 to 9 and call setImage
to set noughts or crosses. This is working as intended.
The problem comes when trying to reset the board, where I call this piece of code:
for i in 1..<10 {
if let button = view.viewWithTag(i) as? UIButton {
button.setImage(nil, for: .normal)
}
}
This should remove the image from the button, but it does nothing. The button is set to Custom
in the storyboard, and the tags are indeed assigned as they should. I also tried getting an outlet for one of the buttons and calling setImage(nil, for: .normal)
in that one and it didn't work either.
I even created a new project with just a button where I call setImage
, and it is indeed working for non-nil images but not with nil
as value.
Has Apple changed the way of removing images? Another question(When button pressed, button.setImage(nil) doesn't work) seems to have the same problem, but if I work with isHidden
I can no longer click on the buttons, and it should not be the workaround, looks very hacky to me.
Is this a bug on iOS? Did the implementation change or something?
Thank you.
CodePudding user response:
This seems like it may be a bug in iOS 15, but it still works if use the new UIButton Configuration API you set its configuration
's image
instead:
button.configuration?.image = nil
Be sure to also configure your button's image through its configuration
property when you want it to have an image on iOS 15, too