Reputation: 905
I'm getting the warning:
'imageEdgeInsets' was deprecated in iOS 15.0
When setting UIButton imageEdgeInsets like so:
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
I tried setting imageEdgeInsets like so:
UIImage(named: "filter")?.withRenderingMode(.alwaysTemplate).withAlignmentRectInsets(UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
Both where top, left, right and bottom are positive or negative values, no luck. Anyone who knows the version of
imageEdgeInsets
That's not deprecated in iOS 15? All help is appreciated, Kind regards :)
Upvotes: 55
Views: 51668
Reputation: 413
Struggle with this for days and finally have a good working solution. Part of my issue is i built the UIButtons in storyboard (without constraints) and then set uiButton title, padding, sizing, and animations such as msg bubbles between users in-game so they move across screen. This should help :)
func MyUI_Btn_Config(_ button: UIButton?, Left: CGFloat?, Right: CGFloat?, Top: CGFloat?, Bottom: CGFloat?, fontSize: CGFloat) {
//size the btn programatically (if need) before calling this function.
let currentInsets = button?.configuration?.contentInsets ?? NSDirectionalEdgeInsets()
let contentInsets = NSDirectionalEdgeInsets(top: Top ?? currentInsets.top, leading: Left ?? currentInsets.leading, bottom: Bottom ?? currentInsets.bottom, trailing: Right ?? currentInsets.trailing)
//1
button?.layoutIfNeeded() //call after size/placement of button, before config.
//2
if button?.configuration == nil {
print("🚨 No Configuration Found > Go To Storyboard editor, set button style to PLAIN for button.title: \(button?.currentTitle ?? "")")
}
//set any UIButton in storyboard inspector to type PLAIN.
var config = button?.configuration ?? .plain() //don't relay on .plain() here, it's not the same as setting in the button, and it's the best fallback strategy.
config.contentInsets = contentInsets
config.attributedTitle?.font = UIFont.systemFont(ofSize: fontSize, weight: .regular)
button?.configuration = config
}
This allows you to pass in NIL for any padding value and set only the new padding value you would like. Ensure to keep the .layoutIfNeeded() call in the function.
tested thoroughly and works!
Upvotes: 0
Reputation: 18994
iOS 15 Apple introduced 3 new options to control padding and insets.
.titlePadding
: Padding between the title and subtitle labels..imagePadding
: Padding between the button’s image and text..contentInsets
: Padding from the button’s content area to its bounds.Using the above option you can manage and set your button style according.
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)
You can also simply write something like ...
let v = UIButton(type: .custom)
v.translatesAutoresizingMaskIntoConstraints = false
v.configuration = .filled()
v.configuration?.imagePadding = 4
Upvotes: 90
Reputation: 1477
Slightly easier fixed, just for this issue:
myButton.configuration?.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20)
//or just imagePadding in the case above:
myButton.configuration?.configuration?.imagePadding = 10
Upvotes: 1
Reputation: 9925
If you need to maintain the original/legacy layout of UIButton and wish to suppress the associated warnings in the Xcode Issue navigator, you can use Key-Value Coding (KVC).
Here's an example of a property wrapper for imageEdgeInsets:
public var imageEdgeInsetsIgnoreDeprecated: UIEdgeInsets {
get {
return value(forKey: "imageEdgeInsets") as! UIEdgeInsets
}
set {
setValue(newValue, forKey: "imageEdgeInsets")
}
}
Upvotes: 2
Reputation: 2042
Swift now comes default with different types of buttons that you can setup up from iOS 15 onwards. Use UIButton.Configuration
to set up your button. For your case, you can use,
config.imagePadding = 5
An example can be seen below:
var filled = UIButton.Configuration.filled()
filled.title = "Filled button"
filled.buttonSize = .large
filled.subtitle = "With subtitle even"
filled.image = UIImage(systemName: "bubble.left.fill")
filled.imagePlacement = .trailing
filled.imagePadding = 5
let button = UIButton(configuration: filled, primaryAction: nil)
Image and Code credits to nemecek.be
Upvotes: 13