SwiftUI Magic: 7 Lesser-Known Features That Will Make Your Apps Stand Out

Experienced iOS and React Native Developer with 10+ Years of Expertise - Available for New Opportunities. Connect Now!
Search for a command to run...

Experienced iOS and React Native Developer with 10+ Years of Expertise - Available for New Opportunities. Connect Now!
No comments yet. Be the first to comment.
This is in continuation of Part 1. V. Customizing Views in SwiftUI A. Overview of the modifiers in SwiftUI Modifiers are a powerful feature of SwiftUI that allow you to customize the appearance and behavior of views. SwiftUI provides a large number o...
This time, I'm exploring the fascinating world of AI, stepping away from my usual focus on iOS development. There's so much more to share about AI, and I can't wait to dive into it with you! If you'd like to follow along and receive regular updates, ...

The app is Time Tango, a Pomodoro timer app. I chose this concept after conducting keyword research for popular terms that face minimal competition on the app store. The app is free to use and received approval in its first review. The strategy focus...

In my last article, I talked about how data flows in SwiftUI. Now, Apple has released something new in iOS 17 - the Observation Framework. This changes how SwiftUI works with data. In this article, we will look at how to use the new Observable Macro ...

One of the critical elements of SwiftUI is its approach to managing data flow within applications. This comprehensive guide will provide you with a strong understanding of data flow in SwiftUI, how it operates, and why it's vital for your SwiftUI app...

Arrays are one of the most common data structures used in programming. In Swift, arrays are used to store ordered lists of values of the same type. They are incredibly versatile and powerful, but using them efficiently is key to writing high-performi...

SwiftUI has taken the world of iOS development by storm, providing an intuitive and powerful way to build beautiful user interfaces across all Apple devices. While many developers have jumped on the SwiftUI bandwagon, there are still several lesser-known features waiting to be discovered.
New to SwiftUI? check out my previous article on Fundamentals of Swift UI.
In this blog post, we will explore 7 of these hidden gems that will truly make your SwiftUI apps stand out.
The matched geometry effect is a powerful tool to create seamless transitions between different views. It enables you to animate the position and size of elements smoothly, providing a stunning visual experience.
struct ContentView: View {
@Namespace private var animation
@State private var isExpanded = false
var body: some View {
VStack {
if isExpanded {
RoundedRectangle(cornerRadius: 20)
.fill(Color.blue)
.frame(width: 200, height: 200)
.matchedGeometryEffect(id: "rectangle", in: animation)
}
Button("Toggle") {
withAnimation {
self.isExpanded.toggle()
}
}
if !isExpanded {
RoundedRectangle(cornerRadius: 20)
.fill(Color.blue)
.frame(width: 50, height: 50)
.matchedGeometryEffect(id: "rectangle", in: animation)
}
}
}
}

Did you know you can create custom environment values to share data across your app? This allows you to keep your code clean and modular.
struct CustomFontKey: EnvironmentKey {
static let defaultValue: UIFont = .systemFont(ofSize: 14)
}
extension EnvironmentValues {
var customFont: UIFont {
get { self[CustomFontKey.self] }
set { self[CustomFontKey.self] = newValue }
}
}
Now, you can set the custom font as a view modifier using .environment():
Text("Hello, SwiftUI!")
.environment(\.customFont, .systemFont(ofSize: 20, weight: .bold))
Or you can use the @Environment property wrapper to get the value from the environment:
@Environment(.customFont) var customFont
.font(Font(customFont))
Do you want to optimize the performance of complex view hierarchies? Use the .drawingGroup() modifier to render your content as a single bitmap, dramatically improving your app's performance.
ZStack {
Circle().fill(Color.red)
Circle().fill(Color.green).offset(x: 10, y: 10)
Circle().fill(Color.blue).offset(x: 20, y: 20)
}
.drawingGroup()
Control which views can receive touch events using the .allowsHitTesting() modifier. This is especially helpful when you want to disable user interaction for specific elements.
Text("Tap me!")
.onTapGesture {
print("Text tapped!")
}
.allowsHitTesting(false)
Importing files from the user's device has never been easier. Use the .fileImporter() modifier to present a file picker and handle the imported files seamlessly.
struct ContentView: View {
@State private var isImporting = false
@State private var selectedFile: URL?
var body: some View {
Button("Import File") {
isImporting = true
}
.fileImporter(isPresented: $isImporting, allowedContentTypes: [.plainText]) { result in
do {
selectedFile = try result.get()
} catch {
print("Error importing file: \(error.localizedDescription)")
}
}
}
}
Use the .overlayPreferenceValue() modifier to create dynamic overlays on your views based on preference values. This can be particularly useful for creating custom progress bars or indicators.
First, create a custom preference key:
struct ProgressPreferenceKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
Now, use .overlayPreferenceValue() to create a dynamic overlay:
struct ContentView: View {
@State private var progress: CGFloat = 0
var body: some View {
VStack {
Slider(value: $progress, in: 0...100)
.overlay(
GeometryReader { geometry in
Color.clear.preference(key: ProgressPreferenceKey.self, value: geometry.size.width)
}
)
Text("Progress: \(Int(progress))%")
}
.frame(width: 200)
.overlayPreferenceValue(ProgressPreferenceKey.self) { totalWidth in
RoundedRectangle(cornerRadius: 5)
.fill(Color.blue)
.frame(width: totalWidth * (progress / 100), height: 10)
}
}
}

Perform asynchronous operations right within your view hierarchy using the .task() modifier. This can be particularly helpful for fetching data from a remote API or performing complex calculations.
struct ContentView: View {
@State private var randomNumber: Int = 0
var body: some View {
VStack {
Text("Random Number: \(randomNumber)")
Button("Fetch") {
// This would typically be replaced with an API call or other asynchronous operation
Task {
await fetchRandomNumber()
}
}
}
}
private func fetchRandomNumber() async {
let random = Int.random(in: 1...100)
try! await Task.sleep(nanoseconds: UInt64.random(in: 1...3) * 1_000_000_000)
DispatchQueue.main.async {
randomNumber = random
}
}
}
By incorporating these hidden gems into your projects, you'll not only level up your SwiftUI skills, but you'll also create apps that truly stand out from the crowd.
I hope you enjoyed this article, and if you have any questions, comments, or feedback, then feel free to comment here or reach out via Twitter.
Thanks for reading!