Skip to content

Instantly share code, notes, and snippets.

struct KeyboardAvoidance: View {
@State var text = ""
var body: some View {
VStack {
// Only work if something is added below the list
TextField("Enter", text: $text)
List {
ForEach(1...50, id: \.self) {
Text("More \($0)")
import SwiftUI
struct RedactedReasonReplaceIsPlaceholder: View {
@Environment(\.redactionReasons) private var redactionReason
let junk: RedactionReasons = .placeholder
var body: some View {
VStack {
Text("Want this to be shown even when redacted!")
.redacted(reason: .init())
import SwiftUI
struct AnimateForever: View {
@State private var flag = false
var body: some View {
VStack {
Rectangle()
// on rectangle, foregroundColor() animate
//
// !!! SwiftUI 3: use .foregroundStyle()! Much simplified
//
import SwiftUI
import os
let logger = Logger(subsystem: "net.hddigitalworks.TextColorAnimate", category: "color")
@mattyoung
mattyoung / Example_Complex.swift
Last active July 31, 2020 10:31 — forked from IanKeen/Example_Complex.swift
PropertyWrapper: @transaction binding for SwiftUI to make changes to data supporting commit/rollback
struct User: Equatable {
var firstName: String
var lastName: String
}
@main
struct MyApp: App {
@State var value = User(firstName: "", lastName: "")
@State var showEdit = false
//
// TextLabelViewForSeanHeber.swift
// NewWorld12
//
// Created by Matthew on 8/1/20.
//
// https://twitter.com/BigZaphod/status/1289674767684046848
import SwiftUI
import SwiftUI
// this demonstrate preference/onPreferenceChange propagate size value
// to set View size do not work. The value is propagated out as can be
// verified by seeing the print() output from closure of .onPreferenceChange()
// but size value is not reflected inside makeBody()
struct CircleProgressViewStyle: ProgressViewStyle, PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
import SwiftUI
struct ContentView: View {
let orientationNotification = NotificationCenter.default
.publisher(for: UIDevice.orientationDidChangeNotification)
.map { _ in
UIDevice.current.orientation
}
// .onReceive() mutate this @State var to trigger re-render on orientation change
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
// accessiblity works normally
VStack {
Text("NavigationLink accessiblilty is fine when not .combine")
import SwiftUI
struct ContentView: View {
@State var value = -122.90
let range = -300.0...300
var body: some View {
VStack {
#warning("`alignment: .firstTextBaseline`, cause the Text inside Label is elided sometime")
HStack(alignment: .firstTextBaseline) {