SwiftUI macOS Preference Pane | Preferences

As easy as 1, 2, 3. Or is it?

1. Create a new SwiftUI file called PreferencePane and add the desired view within a TabView:

import SwiftUI

 

struct PreferencePane: View {

    var body: some View {

        

        TabView {

            Group{

                //ContentsOf the preferences view

                Text(“Profile Settings”)

            }

            .tabItem {

                Label(“General”, systemImage: “gearshape”)

            }

        

            

        }

        .frame(width: 500, height: 280)

        

    }

}

 

2. In your @main app add the following under WindowGroup:  #if os(macOS)… 

import SwiftUI

 

@main

struct NameApp: App {

 

    var body: some Scene {

        

        WindowGroup {

            SomeView

                .environment(\.managedObjectContext, persistenceController.container.viewContext)

 

        }

        

         #if os(macOS)

        Settings {

            PreferencePane()

        }

        #endif

    }

}

 

3. Compile & enjoy!

Screen Shot 2022 03 09 at 5 34 51 PM

Turns out, it was that easy. Done and done!

 

Thoughts, suggestions? Reach me @MarcMasVi

 

Marc