Theme colors in SwiftUI

I want to define theme colors in SwiftUI.

For example, in the default "Watch" app, all letters are orange.

Please tell me how to specify the theme color of the application in this way.

Replies

Hi st0321,

Since there's no concept like UIAppearance in SwiftUI, I'd suggest an approach of defining your reusable themed views and using those across your project instead of basic SwiftUI views, here's an example featuring a Button themed with orange text:

Code Block swift
import SwiftUI
struct MyButton: View {
  private let action: () -> Void
  private let title: String
  init(action: @escaping () -> Void, title: String) {
    self.action = action
    self.title = title
  }
  var body: some View {
    Button(action: self.action) {
      Text(self.title)
        .foregroundColor(.orange)
    }
  }
}
struct MyView: View {
  var body: some View {
    MyButton(action: {
      // your action
    }, title: "Hello world")
  }
}


I hope this helps!
Howdy! You’ll be pleased to hear that with this years releases, your app can adopt an accent color that tints several pieces of your app’s interface. I recommend watching this session when it becomes available:
https://developer.apple.com/wwdc20/10041