Small SwiftUI annoyances

Some (maybe minor) things that I’ve observed while learning SwiftUI and doing some hands on testing:

  • When you present a set of unrelated items in a Picker (i.e. without ForEach) or a set of random tab items in a TabView (in similar conditions) you need to tag() them with integer values if you want selection by index to work. (With ForEach you don’t need to – it seems it does it internally?)
  • When you create a Button you need to pass its action as the first argument of the initializer, and the content as a last closure. But when you create an Alert.Button the Text is expected as the first argument, and the action as a final closure.
  • Corner radius on border modifier is now obsolete. So you are forced to use overlay instead:
    .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.gray))
    Or, if you need a custom background too, this will become an ugly dinosaur with repeating constant:
    .background(Color.yellow)
    .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.gray))
    .cornerRadius(10) // cut background too

    Oh my.
  • scaleEffect modifier will resize the view but layout won’t be affected, i.e. it might be displayed on top of (or behind) other views. The behavior is the same as for offset but there the “effect” is somehow expected…
  • A VStack, HStack, etc. can only contain 10 views added manually into SwiftUI code; you need to use Group to allow more (but that’s OK most of the times).

But there are a lot of good things, too, which I won’t mention since they are everywhere in SwiftUI blog posts, recently. Except for one that I really liked to find out it exists: the .constant binding – very good for prototyping.

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in SwiftUI and tagged . Bookmark the permalink.

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s