'Passing Down @Namespace using ForEach
I'm trying to pass down a namespace using ForEach, however I am getting an error that looks something like this.
Multiple inserted views in matched geometry group Pair<String, ID>(first: "ID", second: SwiftUI.Namespace.ID(id: 73)) have isSource: true, results are undefined.
I am not sure if its because I'm passing the namespace in the ForEach. The thing is that it works but I want to get rid of this error. I am also not sure if I'm passing the namespace correctly.
MRE Code
struct DummyCard: Identifiable {
var id = UUID().uuidString
var title: String
}
let DummyCards: [DummyCard] = [
DummyCard(title: "Dummy Card 1"),
DummyCard(title: "Dummy Card 2"),
DummyCard(title: "Dummy Card 3")
]
struct TestScreen2: View {
@Namespace var namespace
@State var selectedDummyID = ""
@State var didClickBtn: Bool = false
var body: some View {
NavigationView {
ZStack {
VStack {
TabView(selection: $selectedDummyID) {
ForEach(DummyCards) { dummyCard in
DummyCardView(namespace: namespace, dummyCard: dummyCard, didClickBtn: $didClickBtn)
}
}
.tabViewStyle(.page(indexDisplayMode: .always))
.frame(height: 300)
.background(.green)
.padding(.top)
Spacer()
}
.blur(radius: didClickBtn ? 8 : 0)
.overlay(didClickBtn ? .red.opacity(0.3) : .clear)
if didClickBtn {
Text("Make smalller")
.foregroundColor(.white)
.matchedGeometryEffect(id: "btn\(selectedDummyID)", in: namespace)
.frame(width: 300, height: 200)
.background(
RoundedRectangle(cornerRadius: 30)
.fill(.black)
.matchedGeometryEffect(id: "background\(selectedDummyID)", in: namespace)
)
.offset(y: 200)
.onTapGesture {
withAnimation {
didClickBtn = false
}
}
}
}
.navigationTitle("Hello")
.onAppear {
selectedDummyID = DummyCards.first?.id ?? ""
}
}
}
}
struct DummyCardView: View {
var namespace: Namespace.ID
var dummyCard: DummyCard
@Binding var didClickBtn: Bool
var body: some View {
VStack {
Text(dummyCard.title)
Button {
withAnimation {
didClickBtn = true
}
} label: {
Text("Make bigger")
.foregroundColor(.white)
.matchedGeometryEffect(id: "btn\(dummyCard.id)", in: namespace)
.frame(width: 120, height: 44)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(.black)
.matchedGeometryEffect(id: "background\(dummyCard.id)", in: namespace)
)
}
}
}
}
Solution 1:[1]
Your code seems no problem to work fine with trivial correction as shown at the code below on the iOS15.4 Simulator of Xcode13.3.1 environment.
code working image
No error appears during opperating both TabView and Text backgrounded by RoundedRectangle.
+ .background(Color.green)
- .background(.green)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tana005 |
