Last active
July 25, 2022 14:44
-
-
Save masqueNada/38de126f45c6741427f98dcb365de663 to your computer and use it in GitHub Desktop.
Event Card without looking up screen width
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
| // | |
| // JCEventListCard.swift | |
| // JoyKitUI_Example | |
| // | |
| // Created by Jeyashri Natarajan on 1/4/22. | |
| // Copyright © 2022 CocoaPods. All rights reserved. | |
| // | |
| import SwiftUI | |
| import SDWebImageSwiftUI | |
| public struct JCEventListCard: View { | |
| ... | |
| @State private var isPressed: Bool = false | |
| @State private var imageHeight = CGFloat.zero | |
| public var body: some View { | |
| Button { | |
| self.cardSelected?() | |
| } label: { | |
| VStack(alignment: .leading, spacing: JKPaddingConstants.medium) { | |
| let url = (eventImageURL != nil) ? URL(string: eventImageURL!) : nil | |
| if (url == nil) { | |
| Rectangle() | |
| ... | |
| } else { | |
| WebImage(url: url) | |
| .resizable() | |
| .scaledToFill() | |
| .background( | |
| GeometryReader { geo in | |
| // Let a dummy background measure available width and then | |
| // set calculated height on the preference state. | |
| Color.clear | |
| .preference( | |
| key: ViewHeightKey.self, | |
| value: geo.size.width / 1.6 | |
| ) | |
| } | |
| ) | |
| .onPreferenceChange(ViewHeightKey.self) { | |
| // read value from preference in state | |
| self.imageHeight = $0 | |
| } | |
| .frame(height: imageHeight, alignment: .center) | |
| .cornerRadius(JKCornerRadiusConstants.extraSmall) | |
| } | |
| VStack(alignment: .leading, spacing: JKPaddingConstants.extraSmall) { | |
| ... | |
| } | |
| }.padding(.all, JKPaddingConstants.medium) | |
| }.jk_addCardModifier() | |
| } | |
| struct ViewHeightKey: PreferenceKey { | |
| typealias Value = CGFloat | |
| static var defaultValue = CGFloat.zero | |
| static func reduce(value: inout Value, nextValue: () -> Value) { | |
| value += nextValue() | |
| } | |
| } | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment