Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From the blog post "Tiny Hints: a couple of Xcode 11.3 SwiftUI Preview tips"
//
// ContentView.swift
// PreviewsTips2020.01.24
//
// Dad
// @GeekAndDad
//
// Code from blog post:
// https://geekanddad.wordpress.com/2020/01/24/tiny-hints-a-couple-of-xcode-11-3-swiftui-preview-tips/
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
Image(systemName: "paperplane")
.font(.largeTitle)
VStack {
Text("Title Text")
.font(.title)
Text("Subtitle goes here")
.font(.subheadline)
}
}
}
}
struct Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.fixed(width: 400, height: 100))
}
}
// showing the limitation of Dark Mode previews when not in a
// NavigationView
struct PreviewsDarkNotDark: PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.fixed(width: 400, height: 100))
.border(Color.green)
.environment(\.colorScheme, .dark)
}
}
struct PreviewsActuallyDarkMode: PreviewProvider {
static var previews: some View {
ZStack {
Color(.black)
ContentView()
}
.previewLayout(.fixed(width: 400, height: 100))
.environment(\.colorScheme, .dark)
}
}
struct MyPreviewModifer: ViewModifier {
var displayMode: ColorScheme = .light
var name: String = ""
func body(content: Content) -> some View {
ZStack {
Color(displayMode == .light ? .clear : .black)
content
}
.previewLayout(.fixed(width: 400, height: 120))
.previewDisplayName(name)
.environment(\.colorScheme, displayMode)
}
}
struct PreviewsLightDarkDynamicText: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.modifier(MyPreviewModifer())
ContentView()
.modifier(MyPreviewModifer(displayMode: .dark))
ContentView()
.modifier(MyPreviewModifer(displayMode: .dark, name: "dark extraSmall"))
.environment(\.sizeCategory, .extraSmall)
ContentView()
.modifier(MyPreviewModifer(displayMode: .dark, name: "dark extraExtraLarge"))
.environment(\.sizeCategory, .extraExtraLarge)
}
}
}
@GeekAndDadDad

This comment has been minimized.

Copy link
Owner Author

GeekAndDadDad commented Jan 25, 2020

Blog post motivating this and with screenshots is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.