Uibutton Image Behavior Changed in iOS 15

UIButton image behavior changed in iOS 15?

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

imageEdgeInsets' was deprecated in iOS 15.0

iOS 15 Apple introduced 3 new options to control padding and insets.

  1. .titlePadding : Padding between the title and subtitle labels.
  2. .imagePadding : Padding between the button’s image and text.
  3. .contentInsets: Padding from the button’s content area to its bounds.

Using the above option you can manage and set your button style according.

iOS Sample Image 29

You can check this article for more. Source and Image

So your code should be like this

var configuration = UIButton.Configuration.filled()
configuration.title = "Title"
configuration.image = UIImage(systemName: "swift")
configuration.titlePadding = 10
configuration.imagePadding = 10
configuration.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)

Make UIButton Image fit into the Button

I'm assuming you are using Xcode 13 Storyboard designer.

If so, change the Button Style from "Plain":

Sample Image

to "Default":

Sample Image

Now your image will fit to the constrained button size.

UIButton: set image for selected-highlighted state

I found the solution: need to add addition line

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];

Why does a custom UIButton image does not resize in Interface Builder?

While this may not be quite what you're after - the background image does scale.

Custom Drawn UIButton

I was able to get the desired behavior by overriding tintColorDidChange in order to trigger a redraw of the button. Now I am able to draw the outer ring in the correct color (including grayed out when a popover is displayed) while maintaining the desired inner color.

How do I put the image on the right side of the text in a UIButton?

Despite some of the suggested answers being very creative and extremely clever, the simplest solution is as follows:

button.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft

As simple as that. As a bonus, the image will be at the left side in right-to-left locales.

EDIT: as the question has been asked a few times, this is iOS 9 +.

System Image for UIButton appears in iOS14, but not in iOS13

It seems like a bug .

✅ Use this bug as a feature!

Although it is a bug, it's a very good practice to always have a fallback for these kinds of ongoing stuff.

So try to export the symbol (File -> Export..) and import it as an asset to the assets catalog.

Note that although Xcode supports imported symbols, you may consider using a separated icon file (like a PDF) for older iOS devices.


Besides it is more reliable, it will bring some other benefits to your project:

1. Backward compatibility

Since it uses the embedded assets in the bundle, they can be used even in iOS 12 and below.

2. Forward compatibility!

All versions of SF Symbols.app have come out as a beta app and Apple is constantly changing the conventions and rules and other aspects of creating and using these symbols.

For example, take a look at this naming convention:
Demo1

As you can see, The name of this symbol is changed from iOS 15, and old codes will not show this in the future!

3. Wider support

You can use these exported symbols where there is no direct support for using symbols (like UIApplicationShortcutItem when it wasn't supported in the early versions and here is the full example)

Wrap it up:

So it is a good choice to always have backups of symbols and fall back to them if something went wrong...



Related Topics



Leave a reply



Submit