Based on the height of a Text-view, the Card is supposed to either show the complete text (if the input text is short) or display parts of the text and a "Continue reading"-button, which navigates to a Detail-view.
Unfortunately, the GeometryReader doesn't update it's height after the Text-view is rendered (or it does, but the Card-view is not rendered again). OnAppear the geo.size.height is 10, therefore the "Continue reading" code is never executed.
Code:
struct CardView: View {
var title: String = "Title"
var text: String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
var body: some View {
ScrollView { // will be embedded in one on its parentView
GeometryReader { geo in
VStack {
Text(title)
.font(.title2)
Divider()
.padding(.top, -4)
if geo.size.height >= 420 {
VStack {
Text(text)
.frame(height: 340)
Divider()
.padding(.top, -4)
Button(action: {
//button for testing, NavigationLink later
}, label: {
HStack {
Text("Continue reading")
Spacer()
Image(systemName: "chevron.right")
}
.padding(.top, -16)
.frame(height: 44)
.contentShape(Rectangle())
})
.buttonStyle(PlainButtonStyle())
}
} else {
Text(text)
.padding(.bottom, 8)
.fixedSize(horizontal: false, vertical: true)
}
}
.cornerRadius(10)
.padding(.horizontal)
.padding(.top)
.overlay( RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 1))
.padding()
.onAppear{
print(geo.size.height)
}
}
}
}
}
struct CardView_Previews: PreviewProvider {
static var previews: some View {
CardView()
}
}