Skip to content

Instantly share code, notes, and snippets.

View karigrooms's full-sized avatar

Kari Grooms karigrooms

View GitHub Profile
@karigrooms
karigrooms / swiftui-square-image.swift
Last active February 10, 2021 15:59
Making square images in SwiftUI
import SwiftUI
/// Common image aspect ratios
public enum AspectRatio: CGFloat {
case square = 1
case threeToFour = 0.75
case fourToThree = 1.75
}
/// Fit an image to a certain aspect ratio
@karigrooms
karigrooms / swiftui-simple-view.swift
Last active February 17, 2021 18:00
Simple SwiftUI example for Lessons in SwiftUI blog post
import SwiftUI
struct SimpleSwiftUIView: View {
var body: some View {
VStack(alignment: .center, spacing: 8) {
Text("Heading Base")
.headingBase() // internal modifier
Button("Primary Button", action: {})
.buttonPrimaryStyle() // internal modifer
@karigrooms
karigrooms / simple-uikit-view.swift
Last active February 19, 2021 17:32
Simple UIView example for Lessons in SwiftUI blog post
import UIKit
class SimpleUIKitView: UIView {
lazy var button: ButtonPrimary = {
let button = ButtonPrimary() // internal button class
button.title = "Primary Button"
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
@karigrooms
karigrooms / swiftui-uikit-comparison.csv
Created February 17, 2021 18:08
SwiftUI vs UIKit code comparison table for Lessons in SwiftUI blog post
SwiftUI Lines of Code UIKit Equivalent Lines of Code
.headingBase() 43 LabelHeadingBase() 366
.primaryButtonStyle() 188 ButtonPrimary() 792
@karigrooms
karigrooms / swiftui-button-state.csv
Created February 17, 2021 18:54
SwiftUI button state table for Lessons in SwiftUI blog post
Property State Source
isEnabled true/false @Environment
isPressed true/false ButtonStyleConfiguration
@karigrooms
karigrooms / swiftui-lazystack-example.swift
Created February 17, 2021 20:39
SwiftUI lazy stack example for Lessons in SwiftUI blog post
import SwiftUI
@available(iOS 14.0, *)
struct ExampleSwiftUIView: View {
var body: some View {
ScrollView {
LazyVStack {
ForEach(1...50, id: \.self) {
Text("Row \($0)")
}
@karigrooms
karigrooms / swiftui-picker-example.swift
Created February 17, 2021 20:45
SwiftUI picker example for Lessons in SwiftUI blog post
enum ButtonType: String, CaseIterable, Identifiable {
case primary
case secondary
case link
case overlay
}
// View usage
Picker(selection: $selectedButtonType, label: Text("Select a button type")) {
@karigrooms
karigrooms / swiftui-uihostingcontroller-example.swift
Created February 17, 2021 20:50
SwiftUI UIHostingController example for Lessons in SwiftUI blog post
import SwiftUI
import UIKit
class MyViewController: UIViewController {
// this is how you embed a SwiftUI View
lazy var host: UIViewController = {
return UIHostingController(rootView: MySwiftUIView())
}()
@karigrooms
karigrooms / swiftui-uiviewrepresentable.swift
Last active February 19, 2021 17:09
SwiftUI UIViewRepresentable example for Lessons in SwiftUI blog post
import SwiftUI
// UIView wrapped with SwiftUI
struct UserAvatars: UIViewRepresentable {
let users: [UserProfile]
func makeUIView(context: Context) -> UserAvatarsUIView {
return UserAvatarsUIView()
}
@karigrooms
karigrooms / swiftui-composition-order-1.swift
Last active February 19, 2021 18:19
SwiftUI composition order of view modifiers for Lessons in SwiftUI blog post (first example)
import SwiftUI
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.fixedSize()
// apply background color BEFORE size modifiers
.background(Color.blue)