I have custom view as below which i am using in another view
import SwiftUI
public struct TopSheet<Content >: View where Content : View {
private var content: () -> Content
@State private var arrowOffset: Double = 0
public init(@ViewBuilder content: @escaping () -> Content) { self.content = content }
public func expandRatio() -> Double { return max((currentHeight - minHeight) / contentHeight, 0) }
public var body: some View {
HStack(alignment: .center) {
Arrow(offset: arrowOffset)
.stroke(Color.pink, style: StrokeStyle(lineWidth: 4, lineCap: .round, lineJoin: .round))
.frame(width: 30, height: 4)
Spacer()
}
}
}
I am using above view in below view
import SwiftUI
struct PassengerView: View {
@State private var passengers: [String] = ["Joe Black", "Eva Green", "Jared Leto"]
var body: some View {
TopSheet {
VStack {
ForEach($passengers, id: \.self) { passenger in
HStack {
Text(passenger.wrappedValue)
Spacer()
}
}
}
.padding(.horizontal, .afklPaddingL)
}
}
}
Here I want to give one condition Arrow()
from Topsheet
should visible only if passenger count is greater than 1.
I am not sure how should i give this condition as both are in diff view.
CodePudding user response:
Try this:
Add a var to your TopSheet
:
var count: Int
Change the constructor to:
public init(count: Int, @ViewBuilder content: @escaping () -> Content) {
self.count = count
self.content = content
}
and your body to:
public var body: some View {
HStack(alignment: .center) {
if count > 1 {
Arrow(offset: arrowOffset)
.stroke(Color.pink, style: StrokeStyle(lineWidth: 4, lineCap: .round, lineJoin: .round))
.frame(width: 30, height: 4)
}
Spacer()
}
}
call it like:
TopSheet(count: passengers.count) {
VStack {
........
As passengers
is a @State
variable it will reavaluate your views as it changes.