Created
July 22, 2023 20:01
-
-
Save sebj/032c135bf4de5fbe82e58de8165b565f to your computer and use it in GitHub Desktop.
Example: define dynamic colors via SwiftUI's ShapeStyle on iOS 17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
Text("Hello, world!") | |
.foregroundStyle(.customBackground) | |
} | |
} | |
#Preview { | |
ContentView() | |
} | |
// MARK: - Custom Colors | |
extension ShapeStyle where Self == CustomBackgroundColor { | |
static var customBackground: CustomBackgroundColor { .init() } | |
} | |
struct CustomBackgroundColor: ShapeStyle { | |
func resolve(in environment: EnvironmentValues) -> some ShapeStyle { | |
if environment.colorScheme == .light { | |
return Color.red | |
} else { | |
return Color.green | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment