Created
February 25, 2024 17:03
-
-
Save StewartLynch/7d89fb911b27b0231adae633918c4c93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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