In Reader views such as GeometryReader as well as ScrollViewReader, the content area does not seem to detect changes in state correctly.
I googled the issue and found a few similar occurrences, but it wasn't clear to me why this was happening.
I think that a bug exist in some Reader views's content rendering logic.
This is the code that reproduces the behavior.
Changes in state are correctly reflected in areas other than the GeometryReader.
class ViewModel: ObservableObject {
@Published
var show: Bool = false
}
struct Parent: View {
let viewModel = ViewModel()
var body: some View {
Child(viewModel) {
VStack {
GeometryReader { _ in
// Not updated
Text(viewModel.show ? "A" : "B")
}
// Updated
Text(viewModel.show ? "AA" : "BB")
Button("toggle") {
viewModel.show.toggle()
}
}
}
}
}
struct Child<Content: View>: View {
var body: some View {
content()
}
@StateObject
var viewModel: ViewModel
let content: () -> Content
init(_ viewModel: ViewModel, content: @escaping () -> Content) {
self._viewModel = .init(wrappedValue: viewModel)
self.content = content
}
}
Do you have any information on this issue?
Post
Replies
Boosts
Views
Activity
As far as I know, @State (or @Binding) only works within a View.
If used outside of a view, state changes will not be applied to the View or an error will be encountered stating that it has not been installed on a View.
However, in ViewModifiers or ButtonStyles, @State works well, like in the following example
struct MyViewModifier: ViewModifier {
@State
var isHidden: Bool = true
func body(content: Content) -> some View {
VStack {
Button("Toggle Hidden State") { isHidden.toggle() }
if !isHidden {
content
}
}
}
}
How dose it works?
I'm guessing it adopts DynamicProperty, Then it makes sense. However, I couldn't find any adaptation codes on the public interface.
I feel so wronged. In testing, I set accent color to tint color of iOS system color unfortunatly.
I had to check all files, settings for a day.
App was crashed when system set accent of UI. now that I think about it.
Maybe system try to get tint color for accenting, both color cause infinity loop each other.
Do not set app's accent color to tintColor of system color.
https://user-images.githubusercontent.com/11141077/218677717-e5335641-d49c-43f9-9a6b-119c25f62b65.png
I try to override default implementation of view modifier. (Actually it isn't override i know.)
Below code what i expect to work.
struct ContentView: View {
var body: some View {
VStack {
MyCustomView()
}
.foregroundColor(.blue)
}
}
struct MyCustomView: View {
var body: some View {
Rectangle()
.foregroundColor(.yellow)
}
}
I want that MyCustomView's color in ContentView be blue.
I know propagated environment value and override that value can override. (foregroundColor) but i want to give default color or any proeprties.
The other example, custom view has complex view hierarchy. so when foregroundColor gave it, you might apply color theme to whole views based foreground color.
In case custom view has specific property, i can use @Environment, EnvironmentKey, extension EnvironmentValues.
So how can i reuse default view modifier.