Skip to content

Instantly share code, notes, and snippets.

@LarsStegman
Created October 18, 2019 15:08
Show Gist options
  • Save LarsStegman/1123e3b3436fad51f81d1c9412dc6e63 to your computer and use it in GitHub Desktop.
Save LarsStegman/1123e3b3436fad51f81d1c9412dc6e63 to your computer and use it in GitHub Desktop.
Why doesn't the scrollview resize to fit all the contents. Right now, when a cell is tapped and resizes, the VStack size increases and starts falling outside the ScrollView. Shouldn't the ScrollView resize so all the elements stay inside the ScrollView
import SwiftUI
struct ImagePlaceholder: View {
var body: some View {
ZStack {
Rectangle()
.foregroundColor(Color(UIColor.systemGray3))
.aspectRatio(4/3, contentMode: .fill)
Image(systemName: "xmark.rectangle")
.imageScale(.large)
.foregroundColor(Color(UIColor.lightText))
}
}
}
struct WideScreenImage: View {
var body: some View {
Rectangle()
.foregroundColor(Color.green)
.aspectRatio(16/9, contentMode: .fill)
}
}
struct AdaptiveList: View {
var body: some View {
ScrollView {
VStack(alignment: .center) {
ForEach(0..<10) { _ in
AdaptivelyScaledCell()
.fixedSize(horizontal: false, vertical: true)
.background(Color.yellow)
.cornerRadius(10)
}
}
.padding()
}
.background(Color.blue)
}
}
struct AdaptivelyScaledCell: View {
@State var isLoaded: Bool = true
var body: some View {
VStack(spacing: 0) {
if self.isLoaded {
WideScreenImage()
} else {
ImagePlaceholder()
}
HStack {
VStack(alignment: .leading) {
Text("Hello World!")
.font(.headline)
Text("How are you?")
.font(.subheadline)
}
Spacer()
}
.padding()
}.onTapGesture(perform: {
withAnimation {
self.isLoaded.toggle()
}
})
}
}
struct AdaptivelyList_Previews: PreviewProvider {
static var previews: some View {
AdaptiveList()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment