Post

Replies

Boosts

Views

Activity

Reply to ScrollView shrink to fit
Not really sure what your scenario is, so there might be a simpler way. However, I had a somewhat similar situation, but this was without a ScrollView. I found something on Hacking With Swift forum that got me to a solution (bit of a hack) Create a State variable that stores the CGSize of your content Put an overlay on your content view that has a GeometryReader with a Color.Clear view (this is the tacky part) In the onAppear of your Color.Clear, set the CGSize State to your GeometryProxy's size Use a basic .frame modifier to set the size of your Scrollview to the size of the State CGSize struct ContentView: View {     @State private var contentSize: CGSize = .zero     var body: some View {         ScrollView {             Rectangle()                 .fill(Color.blue)                 .frame(width: 100, height: 100)                 .overlay(                     GeometryReader { geo in                         Color.clear.onAppear {                             contentSize = geo.size                         }                     }                 )         }         .background(Color.red)         .frame(maxWidth: contentSize.width, maxHeight: contentSize.height)     } }
Jan ’21
Reply to SwiftUI MapKit Callouts
You can create your own custom AnnotationViews using the MapAnnotation. See below an example that creates a callout with an image. Map(coordinateRegion: $coordinateRegion, annotationItems: annotations) { annotation in    MapAnnotation(coordinate: annotation.coordinate, anchorPoint: CGPoint(x: 0.5, y: 1)) {        AssetAnnotationView(image: annotation.image)    } } Where AssetAnnotationView could look like this: struct AssetAnnotationView: View {     var image: UIImage     var body: some View {         Image(uiImage: image)             .resizable()             .aspectRatio(contentMode: .fill)             .frame(width: 100, height: 100)             .cornerRadius(3.0)             .padding(4)             .callOut()         }     } } callout is a custom modifier here that draws the callout shape (a rounded rect with a beak/point to the bottom of the view)
Feb ’21
Reply to Change size of image based on text SwiftUI
Make sure you put the resizable() modifier on your image (and an .aspectRatio() set to fit). If you have this placed in a VStack or HStack with a fixed size, the size of the image will automatically resize if less space is available because of more lines of text. You can then also give your text a lineLimit() modifier set to 2 to make sure that no more than 2 lines will be displayed. VStack {     Image("Album")         .resizable()         .aspectRatio(contentMode: .fit)     Text("Some long text here that will span two lines")         .lineLimit(2) }.frame(width: 200, height: 200)
Jul ’20