Created
May 2, 2024 08:27
-
-
Save markiv/350a5e96850811035f6c3c2b5a8eff87 to your computer and use it in GitHub Desktop.
Measures the size of any view
This file contains hidden or 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
import SwiftUI | |
struct MeasureSizeModifier: ViewModifier { | |
@Binding var size: CGSize | |
func body(content: Content) -> some View { | |
content.background { | |
GeometryReader { geometry in | |
Color.clear | |
.onAppear { size = geometry.size } | |
.onChange(of: geometry.size) { size = geometry.size } | |
} | |
} | |
} | |
} | |
extension View { | |
func measure(size: Binding<CGSize>) -> some View { | |
modifier(MeasureSizeModifier(size: size)) | |
} | |
} |
Author
markiv
commented
May 2, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment