2026 New Year New Code

  • Jan 4, 2026

2026 New Year, New Code

2026 - Clean up your SwiftUI Code

While updating my books, I decided to also perform some code cleanup.

This is code that still works, but there is now a better or even just a cleaner way of presenting it from when SwiftUI originally came out.

If your projects are 3 to 4 years old or older, you might find some of the same opportunities as I have.

Here are seven examples.

1. Background & Overlay

Before, we had to define shapes and fill them like this:

.background(RoundedRectangle(cornerRadius: 10).fill(Color.blue))

Here is the more modern way:

.background(.blue, in: .rect(cornerRadius: 10))

Other Modifiers

If you wanted to apply an opacity (or other modifiers) to that shape, you might have had code similar to this:

.background(RoundedRectangle(cornerRadius: 10).fill(Color.blue).opacity(0.5))

You would chain the modifiers within the parentheses.

We can use a closure now, though:

.background {
    RoundedRectangle(cornerRadius: 10)
        .fill(Color.blue)
        .opacity(0.5)
}

2. Fonts

There are separate modifiers to specify font and font weight:

Text("Hello, World!")
    .font(.title)
    .fontWeight(.heavy)

But did you know you can combine these modifiers into just one line?

Text("Hello, World!")
    .font(.title.weight(.heavy))

3. Buttons

You might have older Button code that looks something like this:

Button(action: doAction()) {
    Image(systemName: "arrow.right.circle.fill")
}

That's because Xcode's autocomplete would encourage this, but this can be cleaned up to remove the parentheses:

Button { 
    doAction()
} label: {
    Image(systemName: "arrow.right.circle.fill")
}

Text + Symbol

If you defined text and a symbol for your button, for example:

Button(action: { doAction() }) {
    Label("Help", systemImage: "questionmark.circle")
}

Then you can use a more modern button declaration:

Button("Help", systemImage: "questionmark.circle") {
    doAction()
} 

Styles

We didn't always have so many ways to style buttons (or other controls) as we do today, so you might see places where you defined a circle or capsule background for your button:

Button(...)
    .padding()
    .background(Capsule().fill(Color.accentColor))
    .foregroundStyle(.white)

This can be greatly simplified now:

Button(...)
    .buttonStyle(.borderedProminent)

Speaking of styles, many styles, not just for buttons, have also evolved to use that dot syntax you see above. So you might find opportunities to update those too.

For example, your code may have this in it:

Button(...)
    .buttonStyle(BorderedProminentButtonStyle())

This works fine but you can clean it up to use the newer style and promote consitency throughout your projects.

Shapes

You might have used other shapes as backgrounds (besides capsule):

Button(...)
    .padding(8)
    .background(Circle().fill(Color.accentColor))
    .foregroundStyle(.white)

While there is no button style that mimics this, there is a modifier you can add called buttonBorderShape:

 Button(...)
    .buttonStyle(.borderedProminent)
    .buttonBorderShape(.circle)

4. Previews

While this preview will still work, it can be simpler:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The new way is so much cleaner:

#Preview {
    ContentView()
}

5. Assets

You might have code that references assets in your xcassets folder, such as this:

Color("MyCustomColor")
Image("MyImage")

These can be updated to use the dot syntax:

Color(.myCustomColor)
Image(.myImage)

6. Tabs

If your apps use a TabView, there is a good chance your tabs are using the older syntax:

ContentView()
.tabItem {
    Label("Home", systemImage: "house")
}

You can now enclose your tab in a container view:

Tab("Home", systemImage: "house") {
    ContentView()
}

7. File Header Comments

In my opinion, Xcode adds too many comments to the top of our code files.

//
//  AnimationOptions_Duration.swift
//  SwiftUIAnimations
//
//  Created by Mark Moeykens on 11/27/19.
//  Copyright © 2019 Mark Moeykens. All rights reserved.
//

Is any of this helpful to anyone?

Most of this information is redundant (yeah, we know the name of the file, and we can see the other info in source control).

You can update the default Xcode file comment template to just be one line.

Summary

These are the major changes I'm currently updating.

Of course, you can also check for warnings you get when you compile your apps and clean those up as well as look for deprecations and get those updated.