Khoa 🔥 Profile picture
AppStore App of the Day @PastePalApp 🏆 Also made @GetAlmighty @wwdctogether @ChatJoyAI @CodelimeHQ @ShipmunkApp @IndieGoodies

Jun 20, 2022, 11 tweets

✨✨ SwiftUI tips

Y probably using SwiftUI in iOS 15 by now, here are 10 good practices to make good use of #SwiftUI #iosdev

✨ Take advantage of flexible frames

Sometimes we want to show a button in the corner of the screen and we use VStack, HStack with Spacer.

We can use flexible frame with maxWidth, maxHeight. It is more concise and does not have default spacing like in the stack.

✨ Use overlay with ViewBuilder

Let's say we want to show a star icon to the top right of a book cell.

Before iOS 15, overlay and background modifiers accept only View, so we need to perform logic in Group.

Now we can just specify alignment with content ViewBuilder.

✨ Optional conforms to View

Do you want to conditionally show the background only?

I see people use Color.clear for empty case, but remember that Optional also conforms to View if its Wrapped conforms to View.

So we can just return nil instead.

✨ Use dismiss over presentationMode

New in iOS 15, the dismiss EnvironmentValue is the preferred way to dismiss a presentation, sheet, popover, or pop in a navigation stack.

Dismiss is of type DismissAction and implements callAsFunction.

✨ Use continuous curvature rounded corner

With RoundedRectangle, you can specify continuous style to have that "squircle" shape that is more smoothly rounded and looks much more natural.

✨ Multiple trailing closure is convenient

Do you know List and Section in SwiftUI 3 has many new initializers?

The good thing is it reads well with multiple trailing closures. I didn't like it at first but after using for some time, it kinda makes sense.

✨ Concatenate and style Text

Text is SwiftUI is powerful, you can style and concatenate them. Even since SwiftUI 2 in iOS 14, you can also inline Image inside Text

✨ Present sheet with selected item

Do you find yourself using a sheet with isPresented Binding to present something?

Well, sheet has a much better initializer with item Binding, whenever you set a value to this Binding, the sheet will be presented.

✨ Transforming Text and TextField

In case you want to ensure certain casing like uppercase or lowercase in SwiftUI, there's no need to manually altering String with custom Binding.

You can take advantage of existing modifiers like textCase or textInputAutocapitalization

✨ Adjusting color

How often do you use opacity to de-emphasize a View? Or adjust the color of View and Image?

Instead of manually picking some other colors, we can rely on built-in modifiers like brightness, saturation, colorMultiple, and contrast.

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling