Recent articles

Jump to a random post

Adding values to the SwiftUI environment with Xcode 16’s Entry macro

Published on: July 15, 2024

Adding custom values to SwiftUI’s environment has never been very hard to do to. However, the syntax for doing it is verbose and easy to forget. To refresh your mind, take a look at this post where I explain how to add your own environment values to a SwiftUI view. To summarize what’s shown in that post; here’s how you add a custom value to the environment using Xcode 15 and earlier: private struct DateFormatterKey: EnvironmentKey { static let defaultValue: DateFormatter = { let formatter = DateFormatter() formatter.locale = Locale(identifier: "en_US_POSIX") formatter.dateFormat = "MM/dd/yyyy" return formatter }() } extension EnvironmentValues...

Read more...

Let and var in Swift explained

Published on: July 12, 2024

Virtually every programming language will have some means to define properties; Swift does too. We have two approaches to defining a property in Swift. We can use a var or a let. The code below shows how we can define a var or a let as a member of a class: class Member { let id: UUID var name: String init(name: String) { self.id = UUID() self.name = name } } This class has two properties. One is a let, the other is a var. If you're coming from a Javascript background you might expect that there's a third option...

Read more...

Using PreviewModifier to build a previewing environment

Published on: July 10, 2024

Xcode 16 and iOS 18 come with a feature that allows us to build elaborate preview environments using a new PreviewModifier protocol. This protocol allows us to define objects that can create a single context or environment that’s cached and used across your SwiftUI previews. This is useful because it means that you could, for example, populate a database with a bunch of mock data that is then used in your previews. You can also use PreviewModifier to apply specific styling to your previews, to wrap them all in a specific wrapper, and more. Essentially, they’re a tool that allows...

Read more...

Mixing colors in SwiftUI and Xcode 16

Published on: June 18, 2024

SwiftUI in iOS 18 and macOS 15 has gained a new trick; it can mix colors. This means that it’s now possible to take a color and modify it by applying another color to it using a provided percentage. The video below shows how this works: Notice how the large rectangle updates its color to be a certain mix of a left and right color. In the video I use distinct colors but you can also mix with white or black to lighten or darken your color. One use of color mixing I like a lot is to explore color...

Read more...

Using iOS 18’s new TabView with a sidebar

Published on: June 12, 2024

In iOS 18, Apple has revamped the way that tab bars look. They used to be positioned at the bottom of the screen with an icon and a text underneath. Starting with iOS 18, tab bars will no longer be displayed in that manner. Instead, on iPad you will have your tab bar on the top of the screen with text-only items while on iPhone your tab bar will retain its old look. In addition to changing how a tab bar looks, Apple has also added new behavior to the tab bar; it can expand into a sidebar that contains...

Read more...

Building a stretchy header view with SwiftUI on iOS 18

Published on: June 11, 2024

In iOS 18, SwiftUI's ScrollView has gotten lots of love. We have several new features for ScrollView that give tons of control to us as developers. One of my favorite interactions with scroll views is when I can drag on a list an a header image animates along with it. In UIKit we'd implement a UIScrollViewDelegate and read the content offset on scroll. In SwiftUI we could achieve the stretchy header effect with GeometryReader but that's never felt like a nice solution. In iOS 18, it's possible to achieve a stretchy header with little to no workarounds by using the...

Read more...

Modern logging with the OSLog framework in Swift

Published on: June 7, 2024

We all know that print is the most ubiquitous and useful debugging tool in a developer’s toolbox. Sure, we have breakpoints too but what’s the fun in that? Sprinkling some prints throughout our codebase to debug a problem is way more fun! And of course when we print more than we can handle we just add some useful prefixes to our messages and we’re good to go again. What if i told that you can do way better with just a few lines of code. You can send your prints to more places, give them a priority, and more. Of...

Read more...

@preconcurrency usage in swift explained

Published on: May 28, 2024

When you enable strict concurrency checks for your existing projects, it’s likely that Xcode will present loads of warnings and/or errors when you compile your project for the first time. In this post, I’d like to take a look at a specific kind of error that relates to code that you didn’t write. The @preconcurrency declaration can be added to: functions types protocols imports Let’s take a look at all of these areas to fully understand how @preconcurrency helps us enable strict concurrency checks even if we can’t update all of our dependencies just yet. @preconcurrency imports To be specific,...

Read more...

Programmatic navigation in SwiftUI with NavigationPath and navigationDestination

Published on: May 22, 2024

One of the key features that was missing from SwiftUI when it first shipped was a good way to do programmatic navigation. There were some ways to handle this before iOS 16 introduced NavigationPath but it wasn’t very satisfying to use those APIs and they could be rather unreliable at times. To see an example, take a look at this post I wrote about handling deeplinks on iOS 14. In this post, I’d like to revisit programmatic navigation through iOS 16’s NavigationPath API which is a huge leap forward in terms of developer experience and reliability at the same time....

Read more...

Turn off sidebar hiding on NavigationSplitView in SwiftUI

Published on: May 21, 2024

By default, a NavigationSplitView in SwiftUI will show users an option to toggle the visibility of the sidebar. If you want to prevent this button from showing up so that users will always have to see your sidebar, you can do this by applying the toolbar(removing:) view modifier to your split view's sidebar as follows: NavigationSplitView(sidebar: { ExercisesList() .toolbar(removing: .sidebarToggle) }, detail: { ExerciseDetail(exercise: exercises.first!) }) The downside of doing this is that the button is hidden both in portrait and landscape modes. The result is that landscape users can no longer access your app's sidebar. To fix this you...

Read more...