โŒ.readSize()

(deprecated)

SwiftUI โŸฉ Views โŸฉ View โŸฉ .readSize()

โญ๏ธ ๆณจๆ„๏ผšไฝฟ็”จ .readSize() ๆœ‰ๅ€‹ๅคงๅ•้กŒ๏ผŒ่ซ‹็œ‹๏ผš problem with .readSize().

๐Ÿ’ก ๆ็คบ๏ผšๆญคๆ–นๆณ•ๅทฒๆ”นๅ็‚บ .onChangeSize() ใ€‚

// 2022.02.15

extension View {
    /// get parent's offered size.
    /// ```
    /// view.readSize { size in ... }
    /// ```
    func readSize(onChange: @escaping (CGSize) -> Void) -> some View {
        background( GeometryReader { geo in
            Color.clear.preference(key: SizePreferenceKey.self, value: geo.size)
        }).onPreferenceChange(SizePreferenceKey.self, perform: onChange)
    }
}

private struct SizePreferenceKey: PreferenceKey {
    static var defaultValue: CGSize = .zero
    static func reduce(value: inout CGSize, nextValue: () -> CGSize) {}
}

Last updated