Lec05 SwiftUI Tips and Tricks - A Free SwiftUI by Example Tutorial
Lec05 SwiftUI Tips and Tricks - A Free SwiftUI by Example Tutorial
NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 1/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
Note: Constant bindings like this one are just for testing and illustration purposes – you
canʼt change them at runtime.
This allows you to make one screen complete before going on to design the real detail
view.
Read more: How to push a new view onto a NavigationView.
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 2/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
Group {
Text("Row 7")
Text("Row 8")
Text("Row 9")
Text("Row 10")
Text("Row 11")
}
}
}
}
Groups are purely logical containers – they donʼt affect your layouts.
Read more: How to group views together.
While itʼs tempting to always control padding like this to “get things just right”, if you use
the padding() modifier without any parameters you get adaptive padding – padding that
automatically adjusts itself to its content and its environment.
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 3/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
So, if your app is running on an iPad with a regular size class youʼll get more padding than if
the user moves it down to a split view – all without having to write any code.
NEW:HowStart
Read more: my newspacing
to control Ultimate Portfolio
around Appviews
individual courseusing
withpadding.
a free Hacking with Swift+ trial! >>
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 4/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
VStack(alignment: .leading) {
Text("Johnny Appleseed").font(.headline)
Text("Occupation: Programmer")
}
}
}.navigationBarTitle("Users")
}
}
}
Even though itʼs not really that complicated, you still need to read it carefully to understand
what itʼs going on.
Fortunately, we can take parts of the view out into a separate view to make it easier to
understand and easier to re-use, and Xcode makes it a cinch: just Cmd-click on the
navigation link and choose Extract Subview. This will pull the code out into a new SwiftUI
view, and leave a reference where it was.
Note: If your subview relies on data from the parent youʼll need to pass that along yourself.
Read more: How to create and compose custom views.
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 5/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
Better previewing
NEW:benefits
One of the many Start my new Ultimate
of SwiftUI is that wePortfolio Apppreviews
get instant course with
of oura layouts
free Hacking
as we with Swift+ trial! >>
work. Even better, we can customize those previews so that we can see multiple designs
side by side, see how things look with a navigation view, try out dark mode, and more.
For example, this creates a preview for ContentView that shows three different designs
side by side: extra large text, dark mode, and a navigation view:
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.environment(\.sizeCategory, .accessibilityExtra
ContentView()
.environment(\.colorScheme, .dark)
NavigationView {
ContentView()
}
}
}
}
#endif
Tip: Make sure you zoom out or scroll around in the preview window to see all the different
previews.
Read more: How to preview your layout in different devices.
You can now attach that to any view using .modifier(PrimaryLabel()), like this:
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 6/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
if showingWelcome {
Text("Hello World")
}
}
}
}
When the toggle is changed, the text view below it will appear or disappear immediately,
which isnʼt a great experience. However, if we used animation() we could make the view
slide in and out smoothly when the toggle is changed:
Toggle(isOn: $showingWelcome.animation()) {
Text("Toggle label")
}
You can even control the kind of animation you want, like this:
Toggle(isOn: $showingWelcome.animation(.spring())) {
Text("Toggle label")
}
When youʼre working with regular state rather than bindings, you can animate changes by
wrapping them in a withAnimation() call.
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 7/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
For example, hereʼs our same view except now it shows or hide the welcome label using a
button press:
NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>
struct ContentView: View {
@State private var showingWelcome = false
if showingWelcome {
Text("Hello World")
}
}
}
}
As with before that will cause the welcome label to appear and disappear immediately, but
if we wrap our changes in withAnimation() they will be animated instead:
withAnimation {
self.showingWelcome.toggle()
}
Showing multiple
If you try to attach multiple
alerts in a view
modifiers to a single view, youʼll find your code
alert()
doesnʼt work as you expect – one alert will work but the other wonʼt.
To fix this, you should attach your alerts to different parts of your view hierarchy, such as to
the button or other view that triggers the alert to appear.
Read more: How to show multiple alerts in a single view.
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 8/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
As with UIKit and most other UI frameworks, you can do all the background work you want
in your SwiftUI apps, but you should only ever manipulate the user interface on the main
thread. Because stateStart
changes automatically trigger aApp
refresh of your body, itʼs important
that you make sure you perform those state changes on the main thread.a free Hacking with Swift+ trial! >>
NEW: my new Ultimate Portfolio course with
Read more: How to use @ObservedObject to manage state from external objects.
SPONSORED Are you tired of wasting time debugging your Swift app? Instabugʼs SDK
is here to help you minimize debugging time by providing you with complete device
details, network logs, and reproduction steps with every bug report. All data is
attached automatically. It only takes a line of code to setup. Start your free trial now
and ship quality apps!
Try it for free
Sponsor Hacking with Swift and reach the world's largest Swift community!
Similar solutions…
All SwiftUI property wrappers explained and compared
How to use Instruments to profile your SwiftUI code and identify slow layouts
Answering the big question: should you learn SwiftUI, UIKit, or both?
Building a menu using List
Frequently asked questions about SwiftUI
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 9/10
2/1/2021 SwiftUI tips and tricks - a free SwiftUI by Example tutorial
@twostraws
paul@hackingwithswift.com
Sponsor the site
About Glossary Privacy Policy Refund Policy Update Policy Code of Conduct
Thanks for your support, Herman C Vermeulen!
Swift, the Swift logo, Swift Playgrounds, Xcode, Instruments, Cocoa Touch, Touch ID, AirDrop, iBeacon, iPhone, iPad, Safari, App Store,
watchOS, tvOS, Mac and macOS are trademarks of Apple Inc., registered in the U.S. and other countries. Pulp Fiction is copyright © 1994
Miramax Films. Hacking with Swift is ©2021 Hudson Heavy Industries.
https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tips-and-tricks 10/10