Post

Replies

Boosts

Views

Activity

Reply to How to share @Namespace between views
You can declare variable of Namespace.ID in a view to reference @Namespace from its parent. Here is an example. struct ContentViewNameSpace: View { 		@State private var isExpanded = false 		@Namespace private var namespace 		 		 		var body: some View { 				Group() { 				if isExpanded { 						VerticalView(namespace: namespace) 				} else { 						HorizontalView(namespace: namespace) 				} 				}.onTapGesture { 				withAnimation { 						isExpanded.toggle() 				} 				} 		} } struct VerticalView: View { 		var namespace: Namespace.ID 		 		var body: some View { 				VStack { 				RoundedRectangle(cornerRadius: 10) 						.foregroundColor(Color.pink) 						.frame(width: 60, height: 60) 						.matchedGeometryEffect(id: "rect", in: namespace, properties: .frame) 				Text("Hello SwiftUI!").fontWeight(.semibold) 						.matchedGeometryEffect(id: "text", in: namespace) 				} 		} } struct HorizontalView: View { 		var namespace: Namespace.ID 		 		var body: some View { 				HStack { 				Text("Hello SwiftUI!").fontWeight(.semibold) 						.matchedGeometryEffect(id: "text", in: namespace) 				RoundedRectangle(cornerRadius: 10) 						.foregroundColor(Color.pink) 						.frame(width: 60, height: 60) 						.matchedGeometryEffect(id: "rect", in: namespace, properties: .frame) 				} 		} }
Jul ’20