What is the replacement for deprecated contentEdgeInsets

Warning : <<'contentEdgeInsets' was deprecated in iOS 15.0: This property is ignored when using UIButtonConfiguration>>

I don't use SwiftUI so what is the replacement for contentEdgeInsets? None of the UIButtonConfiguration or UIButton.Configuration.filled() exist.

I need to be able to change the inset dynamically.

Answered by ShaddamIV in 715817022

Problem

        //Warning : 'contentEdgeInsets' was deprecated in iOS 15.0:
        //           This property is ignored when using UIButtonConfiguration
        /*uiButton.contentEdgeInsets = UIEdgeInsets(top:    8.0, left:   8.0, bottom: 8.0,right:  8.0 )

Solution

uiButton.configuration!.contentInsets = NSDirectionalEdgeInsets(top: 8.0, leading: 8.0, bottom: 8.0, trailing: 8.0)

Apple has finally given access to "uiButton.configuration!.contentInsets" in their latest Xcode update. It now woks on iOS and macCatalyst.

UIButton.Configuration has a property named contentInsets. As far as I tried, it can be dynamically changed.

        button.configuration?.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20)

Does not work, I get the error : Value of type 'UIButton' has no member 'configuration'

This line of code : var configuration = UIButton.Configuration.filled()

will generate the error : Type 'UIButton' has no member 'Configuration'

As I said above, UIButton.Configuration does not exist and it should.

Yes, my project is iOS 15 and is also Mac Catalyst. So I only have 1 code for iOS and macOS, no project settings switching.

I have Xcode 13.0, you are right, I can't set macOS 12. So I guess I must wait for Apple to fix it in the next Xcode? So eventually it will resolve itself with the solution you gave me. Thank you good sire.

Now it is working on macOS 12.0

Problem is as soon as you use anything from Configuration, everything else stop working. There is obviously a conflict in that SDK. 2 mutually exclusive things in the same UIButton.

Also if I use configuration to set image, there is only 1 image instead of the 2 that you could set on the good old UIButton. I will wait for Apple to make their mind about where the are going. I will let the messy storm pass.

It is now working, but everything else stop working (I am not the only one reporting that). Every parameters of UIButton will be ignored if you use Configuration. Also Configuration is too limited compared to good old button. Many features missing like image for when it is pressed. I will let Apple clean this mess before desecrating my code.

Accepted Answer

Problem

        //Warning : 'contentEdgeInsets' was deprecated in iOS 15.0:
        //           This property is ignored when using UIButtonConfiguration
        /*uiButton.contentEdgeInsets = UIEdgeInsets(top:    8.0, left:   8.0, bottom: 8.0,right:  8.0 )

Solution

uiButton.configuration!.contentInsets = NSDirectionalEdgeInsets(top: 8.0, leading: 8.0, bottom: 8.0, trailing: 8.0)

Apple has finally given access to "uiButton.configuration!.contentInsets" in their latest Xcode update. It now woks on iOS and macCatalyst.

Ignore my comment above, it does not work at all. Can't delete and cannot edit my previous post.

As indicated, the accepted answer will result in a crash unless the configuration has already been set.

let button = UIButton()
var configuration = UIButton.Configuration.plain() // there are several options to choose from instead of .plain()
configuration.contentInsets = NSDirectionalEdgeInsets(top: 24, leading: 24, bottom: 24, trailing: 24)
button.configuration = configuration

You may have to set additional properties on configuration to maintain the current look of your button. To change it dynamically I believe you just give your button a new configuration.

What is the replacement for deprecated contentEdgeInsets
 
 
Q