Post

Replies

Boosts

Views

Activity

ScrollViews Sync SwiftUI
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)
1
0
1.6k
Mar ’21
Preference tried to update multiple times (SwiftUI)
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() } }
1
0
2k
Apr ’21
MatchedGeometryEffect in case of .size
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() } }
0
0
411
Apr ’21