Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton image behavior changed in iOS 15?

My code is very simple; I have an outlet to a UIButton, button, and I am setting its image in code:

    let jack = UIImage(named:"jack.png")
    self.button.setImage(jack, for:.normal)

The problem is that this is not behaving as I expect. I expect the button image to be sized down to the button size, and I expect it to be a template image (tinted with the inherited tint color). Instead, I'm seeing the original image and it is full-sized. Is this a change in iOS 15?

It seems to be, because if I set my project's deployment target to iOS 14 and run it on an iOS 14 simulator, I do get the behavior that I expect.

like image 217
matt Avatar asked Oct 04 '21 16:10

matt


1 Answers

Is this a change in iOS 15?

Yes and no. There is indeed a change in iOS 15, but the reason for the problem you're experiencing is a change in Xcode 13.

The change in iOS 15 is that there's a whole new way of configuring a button. This starts with giving the button one of four new iOS 15 types: Plain, Gray, Tinted, and Filled. If you set your button to have any of those types, you are opting in to the new behavior.

The problem you're seeing is because, in Xcode 13, when you make a button in the storyboard, it does give the button one of those types: Plain. So you have opted into the new dispensation without knowing it!

The solution, if you want the old behavior, is to change the Style pop-up menu (in the Attributes inspector) from Plain to Default. Now you have an old-style button and it will behave in the way you're accustomed to.

(Of course, in the long run, you're going to want to adopt the new dispensation. I'm just explaining the apparent change in behavior.)

like image 90
matt Avatar answered Nov 15 '22 20:11

matt