Khoa 🔥 Profile picture
Jun 20, 2022 11 tweets 4 min read Read on X
✨✨ 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.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Khoa 🔥

Khoa 🔥 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @onmyway133

Jul 27, 2022
SwiftUI helps make apps faster, and I've started to use it in all my production apps

Here are some #SwiftUI tips I've learned along the way that will help you write better SwiftUI apps ✨✨
✨ There is a strokeBorder method

Some shape like Circle conforms to InsettableShape protocol, which provides strokeBorder method.

The stroke method strokes inside and outside the border.
The strokeBorder insets the stroke by lineWidth / 2 Image
✨ Take advantage of Publisher in Combine

Both State and StateObject attaches to the lifetime identity of a SwiftUI view.

In cases where we need to adjust the stream, I prefer using ObservableObject. Here is how I debounce the Textfield text to perform searching. Image
Read 8 tweets
Apr 16, 2022
Are you new to iOS and Swift and want to find good blogs to learn & get up to date?

I can recommend the following 21 best free resources and blogs to learn Swift and SwiftUI for iOS developers #iosdev 👩‍💻🧑‍💻
⚡️ Stanford University's course CS193p (Developing Applications for iOS using SwiftUI)
cs193p.sites.stanford.edu

⚡️ Raywenderlich
raywenderlich.com

⚡️ ObjC Issues
objc.io/issues/

⚡️ NSHipster
nshipster.com

⚡️ 100 days of Swift
hackingwithswift.com/100
⚡️ Learn to code (Apple)
developer.apple.com/learn/curricul…

⚡️ App Dev Training (Apple)
developer.apple.com/tutorials/app-…

⚡️ Kavsoft
kavsoft.dev

⚡️ Sarun by @sarunw
sarunw.com

⚡️ Swift by Sundell
swiftbysundell.com

⚡️ Donny Wals by @DonnyWals
donnywals.com
Read 5 tweets
Apr 11, 2022
What are some new things you learn about #SwiftUI lately?

Over the course of making several SwiftUI apps, I've discovered quite a few hidden magic of SwiftUI that are quite fun.

Here are 6 interesting SwiftUI features in View Builder many don't know are even possible 🤯
🤯 View protocol with enum

Struct is not the only way to describe #SwiftUI views. Learn how you can achieve the same thing with just enum
🤯 Conform primitive type to View protocol

You can also conform any value type or primitive type to View protocol

The only requirement View needs is to yield a reasonable body.
Read 7 tweets
Apr 5, 2022
Have you recently discovered some nifty UIKit APIs that you wish you had known earlier?

Apple introduces new changes every WWDC but it may take a while until they can be adopted in production, hence developers forgot.

Here are 9 useful APIs from iOS 7 - 15 many fail to notice
keyboardDismissMode, iOS 7

Have you tried making the keyboard dismissal follow along drag gesture? It's not an easy feast but I still see manual implementation in some codebases.

They didn't know that in iOS 7, you can specify keyboardDismissMode to be .interactive. Image
NSLayoutConstraint.active, iOS 8

I know everyone should be using NSLayoutAnchor and not the tedious NSLayoutConstraint.

Before iOS 8, we needed to find the view that is the closest common ancestor of the items managed by the constraint, and it is a very tricky task. Image
Read 10 tweets
Mar 17, 2022
Development & design tools for iOS developers #iosdev ⚡️🔥

These are the 33 tools I use daily and have saved me so much time that I can't recommend them enough.
🔥 WTF Auto Layout
Demystify Auto Layout constraint error logs
wtfautolayout.com

🔥 NSDateFormatter
Preview different date formats
nsdateformatter.com

🔥 Quick Type
Convert JSON to Swift code
app.quicktype.io

🔥 Screen Sizes
Device info
screensizes.app
🔥 Cubic Bezier
Play around with bezier path
cubic-bezier.com

🔥 Regex101
Test regular expression
regex101.com
Read 10 tweets
Mar 16, 2022
Swift is a very typesafe and expressive language.

With newer features mean we can find new ways to write even more concise Swift code.

Below are 5 sweet techniques that you can use to supercharge your codebase #iosdev 💪🤩
💪 allSatisfy

Instead of using filter and count, you can use allSatisfy to check whether all element satisfies a given condition Image
💪 keyPath

Available since Swift 5.2, keyPath is a type that references a property, allowing us to write much cleaner code.

Given that it is a type, we can store keyPath, pass it around, and combine them. Image
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(