17

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141

1 Answers1

41

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.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 3
    I was going nuts before I figured this out. If you wish old-style UIButton behaviour (Xcode 13, iOS 15) in storyboard, after adding UIButton change Style -> Default. – sabiland Oct 14 '21 at 08:45
  • I wish I could upvote this more than once, wasted so much time trying to figure out what was happening – Zaheer Moola Nov 29 '21 at 18:46
  • Okay, but how do you do this with code? There is no `UIButton.ButtonType` case called `default` – Adam Dec 18 '21 at 12:47
  • The same way you always did. Just don't opt in to the new button configurations. – matt Dec 18 '21 at 15:32
  • You're right, I was convinced that the configuration is assigned by default on iOS 15 but it turned out, I was doing it in my custom UIButton initializer. Few hours lost but I can finally make a square shaped button with image again, yay! – Adam Dec 19 '21 at 11:24
  • In addition UIButton tags do not work in XCode 13 and iOS 15 combination. The tag you give to UIButton does not stick! Tag changes itself in a very strange manner. But if you keep the tag in a variable then it sticks. So I think there may be even more strange things around this. – Ahmet Akkök Dec 29 '21 at 21:08
  • Can I marry you? Thanks for ending my hair pulling out session you saved just enough for me to try and make a comb over work. – JohnAnge Kernodle Apr 27 '22 at 17:50
  • If we do want to adopt the new style, what is the solution? Like the OP, my button images are not the correct size. – zakray Dec 20 '22 at 23:09