Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Created February 25, 2024 17:03
Show Gist options
  • Save StewartLynch/7d89fb911b27b0231adae633918c4c93 to your computer and use it in GitHub Desktop.
Save StewartLynch/7d89fb911b27b0231adae633918c4c93 to your computer and use it in GitHub Desktop.
//
// Created for MyWeather
// by Stewart Lynch on 2024-02-24
//
// Follow me on Mastodon: @[email protected]
// Follow me on Threads: @StewartLynch (https://www.threads.net)
// Follow me on X: https://x.com/StewartLynch
// Follow me on LinkedIn: https://linkedin.com/in/StewartLynch
// Subscribe on YouTube: https://youTube.com/@StewartLynch
// Buy me a ko-fi: https://ko-fi.com/StewartLynch
import SwiftUI
extension View {
func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
background(
GeometryReader { geometryProxy in
Color.clear
.preference(key: SizePreferenceKey.self, value: geometryProxy.size)
}
)
.onPreferenceChange(SizePreferenceKey.self, perform: onChange)
}
}
struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment