View+innerSize.swift (3342B)
1 /* MIT License 2 * Copyright (c) 2023 mevmev, Rahul Bir + Michal Šrůtek, jnpdx 3 * https://stackoverflow.com/questions/74471576/make-sheet-the-exact-size-of-the-content-inside 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in all 13 * copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 /** 24 * @author Marc Stibane 25 */ 26 import SwiftUI 27 28 fileprivate struct InnerHeightKey: PreferenceKey { 29 static var defaultValue: CGFloat { 10 } 30 static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { 31 value = nextValue() 32 } 33 } 34 35 fileprivate struct InnerWidthKey: PreferenceKey { 36 static var defaultValue: CGFloat { 10 } 37 static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { 38 value = nextValue() 39 } 40 } 41 42 fileprivate struct InnerSizeKey: PreferenceKey { 43 static var defaultValue: CGSize { CGSize(width: 10, height: 10) } 44 static func reduce(value: inout CGSize, nextValue: () -> CGSize) { 45 value = nextValue() 46 } 47 } 48 49 extension View { 50 func innerHeight(_ height: Binding<CGFloat>) -> some View { 51 overlay { // background doesn't work for sheets 52 GeometryReader { proxy in 53 Color.clear 54 .preference(key: InnerHeightKey.self, value: proxy.size.height) 55 } 56 } 57 .onPreferenceChange(InnerHeightKey.self) { newValue in 58 // print("❗️InnerHeight \(newValue)❗️") 59 height.wrappedValue = newValue 60 } 61 } 62 63 func innerWidth(_ width: Binding<CGFloat>) -> some View { 64 overlay { // background doesn't work for sheets 65 GeometryReader { proxy in 66 Color.clear 67 .preference(key: InnerWidthKey.self, value: proxy.size.width) 68 } 69 } 70 .onPreferenceChange(InnerWidthKey.self) { newValue in 71 // print("❗️InnerWidth \(newValue)❗️") 72 width.wrappedValue = newValue 73 } 74 } 75 76 func innerSize(_ size: Binding<CGSize>) -> some View { 77 overlay { // background doesn't work for sheets 78 GeometryReader { proxy in 79 Color.clear 80 .preference(key: InnerSizeKey.self, value: proxy.size) 81 } 82 } 83 .onPreferenceChange(InnerSizeKey.self) { newValue in 84 // print("❗️InnerSize \(newValue)❗️") 85 size.wrappedValue = newValue 86 } 87 } 88 }