Skip to content

Instantly share code, notes, and snippets.

View BottomSheet.swift
import SwiftUI
struct ContentView: View {
@State var show: Bool = false
var body: some View {
ZStack {
Button {
show.toggle()
} label: {
View SwiftAttributedStrings.swift
struct ContentView: View {
var body: some View {
Text(attributedString)
}
var attributedString: AttributedString {
var attributes = AttributeContainer()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 10.0
View codable-swift.md

If I have a JSON Codable object in Swift that looks like:

struct IceCreamStore: Codable {
    let iceCreams: [IceCream: Int]
}

enum IceCream: String, Codable {
    case chocolate, vanilla
}
View adaptive-scrollview.swift
import SwiftUI
struct ContentView: View {
var body: some View {
ViewThatFits {
MyCoolVStack()
ScrollView(.vertical) { MyCoolVStack() }
}
.background(Color.gray)
}
View UIKitVersusSwiftUIAnimationFrameRate.swift
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let box = UIView()
box.frame = CGRect(x: (view.bounds.width / 2.0) - 100.0, y: 0.0, width: 100.0, height: 100.0)
box.backgroundColor = .systemGreen
View IceCreamPreferenceKey.swift
import SwiftUI
enum ShopState: Int {
case iceCream, flowers
}
struct ShopStatePreferenceKey: PreferenceKey {
static var defaultValue: ShopState = .iceCream
static func reduce(value: inout ShopState, nextValue: () -> ShopState) {
View SwiftUI-Image-Sizing.swift
struct ContentView: View {
var body: some View {
List {
Section {
VStack(alignment: .leading, spacing: 20.0) {
Image("example") // Natively 1179x787
.resizable()
.scaledToFill()
VStack(alignment: .leading, spacing: 20.0) {
View PaddingRangeSwiftUI.swift
var body: some View {
Color.black.opacity(0.2)
.frame(width: 180.0, height: 60.0)
.overlay(
VStack(alignment: .leading, spacing: 0.0) {
Text(input)
.font(.system(size: 12.0, weight: .semibold))
.layoutPriority(1.0)
Spacer(minLength: 0.0)
View VariablePaddingSwiftUI.swift
import SwiftUI
struct ContentView: View {
let text1 = "Hello this is some very long text that goes on for quite awhile which can make it tricky to display"
let text2 = "I am brief"
var body: some View {
Color.black.opacity(0.2)
.frame(width: 180.0, height: 60.0)
.overlay(
View SKAnimatedAction.swift
import UIKit
import SpriteKit
class SKAnimatedAction {
let movementAction: SKAction
let animationAction: SKAction
let animationKey: String
init(movementAction: SKAction, animationAction: SKAction, animationKey: String) {
self.movementAction = movementAction