Wie ändere ich die Bildgröße einer Schaltfläche mit der Schaltflächenkonfiguration?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie ändere ich die Bildgröße einer Schaltfläche mit der Schaltflächenkonfiguration?

by Guest » 06 Jan 2025, 21:59

Ich verwende diesen Code, um eine Schaltfläche mit einem Bild zu erstellen. Und ich möchte, dass die Bildgröße mit der Schaltflächengröße übereinstimmt. Ich verwende dafür diesen Code:

Code: Select all

func buttonImage() {
var configuration = UIButton.Configuration.plain()
configuration.image = UIImage(named: "someImage")!
configuration.baseBackgroundColor = .red
configuration.contentInsets = .zero

let button = UIButton(configuration: configuration)
button.backgroundColor = .red
button.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(button)

button.widthAnchor.constraint(equalToConstant: 300).isActive = true
button.heightAnchor.constraint(equalToConstant: 300).isActive = true
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
aber ich habe dieses Ergebnis:
[img]https://i.sstatic.net /MPBRE3pB.png[/img]

Alles funktioniert gut, wenn ich eine Einzelskala mit 135x135 ohne @3x-Suffix anstelle von Einzelskalen mit 3 Bildern 45x45 verwende. 90x90, 135x135 in meinen Assets.
Wie kann ich das Problem mithilfe der Konfiguration beheben?

Top