I'm trying to get the same view for the first column as the second one using MatchedGeometryEffect in applying only property .size but the behavior is not as I expect. The elements of the first column replicate the size but positions are strange as you can see on the screen. What might be wrong ? or this is a bug ?!
the behavior is the same even if I put all elements in the same VStack
Sorry did not find how to place screens here
The screens are here - https://forums.swift.org/t/matchedgeometryeffect-in-case-of-size/47486
import SwiftUI
struct ContentView: View {
@Namespace var ns
var body: some View {
HStack {
//first column
ScrollView {
VStack(spacing: 0) {
Rectangle()
.matchedGeometryEffect(id: "ID1", in: ns, properties: .size, isSource: false)
Rectangle()
.matchedGeometryEffect(id: "ID2", in: ns, properties: .size, isSource: false)
}.background(Color.green)
}
//second column
ScrollView {
VStack(spacing: 0) {
Rectangle().frame(width: 100, height: 25)
.border(Color.blue)
.matchedGeometryEffect(id: "ID1", in: ns, properties: .size, isSource: true)
Rectangle().frame(width: 100, height: 50)
.border(Color.blue)
.matchedGeometryEffect(id: "ID2", in: ns, properties: .size, isSource: true)
}.background(Color.red)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Post
Replies
Boosts
Views
Activity
I measure the child view size and pass it to the parent view using preference technic. Time by time I get the next message in console Bound preference WidthKey tried to update multiple times per frame.
Nothing happens in terms of errors or an unexpected behavior.
I get that the nature of TextField is asynchronous and this is might be the reason The question is could it be the problem in more complex examples, layouts ?! or it's just an information about the behavior
there are video examples reproducing the message - https://forums.swift.org/t/preference-tried-to-update-multiple-times-swiftui/47451/6
import SwiftUI
struct ContentView: View {
@State var width: CGFloat? = nil
@State var myText : String = "Text"
var body: some View {
VStack{
TextField("Mytext", text: $myText)
Spacer()
Text("\(myText)")
.background(
GeometryReader{ proxy in
Color.clear.preference(key: WidthKey.self, value: proxy.size.width)
}
)
.fixedSize()
.frame(width: 200, height: 200)
.onPreferenceChange(WidthKey.self){self.width = $0 }
}
}
}
struct WidthKey: PreferenceKey {
static var defaultValue: CGFloat? = nil
static func reduce(value: inout CGFloat?, nextValue: () - CGFloat?) {
value = value ?? nextValue()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Any ideas how to detect local coordinates within a view in SwiftUI under the mouse cursor?! The question is in the context for macOS apps
Hi, I've got two ScrollViews I need to sync vertical scrolls of these views. When I scroll one the other one should automatically be adjusted to the new scroll position of first one.
Do you have any idea how to implement this behavior ?
I googled I guess the only way to navigate(to scroll to) within scrollView it's through
ScrollViewReader
_proxy.scrollTo(ID_element_in_ScrollView, anchor: .center)
Hi, I came across an error (xcode 12 beta 5) if I want to add any picture(svg) in the assets I cant build projects and get an Error "ERROR: Each TDDistiller instance can be distilled only one time!" As long as I delete what I added everything is [OK.] I tired to clean build but it does not help. Any ideas ?