# SwiftUI Favorite Button Constructor

**URL:** <https://ifnotnil.com/t/swiftui-favorite-button-constructor/4282>\
**Category:** Swift\
**Tags:** swiftui\
**Created:** [6 March 2025 04:36 UTC](https://ifnotnil.com/t/swiftui-favorite-button-constructor/4282 "2025-03-06T04:36:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [6 March 2025 04:36 UTC](https://ifnotnil.com/t/swiftui-favorite-button-constructor/4282/1 "2025-03-06T04:36:59Z")

</div>

One thing I don’t like about Swift and SwiftUI is overloaded constructors, there’s at least 3 ways to create a button.

What’s your favorite way?

Compact constructor.

```auto
Button( "Label" ) {
 // code goes here
}

or

Button( "Label", systemImage: "plus" ) { // code goes here }

```

Action first then label

```auto
Button( action: { // code goes here } ) {
 HStack{ 
  Image( systemName: "plus" )
  Text( "Label" )
 }
}

```

Another way of action first then label.

```auto
Button {
 // code goes here
} label: {
 Text( "Label" )
}

```

The last two allow for far more control over the button label, including images that are not SF Symbols, but I think I prefer the compact version.

---

<div class="post-metadata">

**Author:** ![Torsten\_B](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@Torsten\_B](https://ifnotnil.com/u/Torsten_B)\
**Post date:** [6 March 2025 11:54 UTC](https://ifnotnil.com/t/swiftui-favorite-button-constructor/4282/2 "2025-03-06T11:54:09Z")

</div>

It depends on what you want to achieve. There is a great choice of options but we need to keep that stuff in our brain…  
I prefer a somewhat overwhelming number of options to a bare-bones system where I get stuck if something beyond very basic UI functionality is needed.

---

<div class="post-metadata">

**Author:** ![markus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/m/b782af/32.png) [@markus](https://ifnotnil.com/u/markus)\
**Post date:** [6 March 2025 16:54 UTC](https://ifnotnil.com/t/swiftui-favorite-button-constructor/4282/3 "2025-03-06T16:54:45Z")

</div>

Sometimes compact, often the 3. example. For a simple ‘Quit’, I’d use of the compact one…

Yes, the constructors are not always ‘best’

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [7 March 2025 02:35 UTC](https://ifnotnil.com/t/swiftui-favorite-button-constructor/4282/4 "2025-03-07T02:35:11Z")

</div>

Thankfully the auto complete in Xcode and the Swift language make having multiple constructors much easier than it was with Xojo.
