I'm Trying to Implement a View Stack in Swiftui and My @State Objects Are Being Reset for Reasons That Are Unclear to Me

SwiftUI ViewModifier not working as a NavigationLink

The modifier doesn't work because the content argument is not the actual view being modified, but instead is a proxy:

content is a proxy for the view that will have the modifier represented by Self applied to it.

Reference.

This is what a quick debugging over the modifier shows:

(lldb) po content
SwiftUI._ViewModifier_Content<SwiftUIApp.NavLink>()

As the proxy is an internal type of SwiftUI, we can't know for sure why NavigationLink doesn't work with it.

A workaround would be to skip the modifier, and only add the extension over View:

extension View {
func navLink(title: String) -> some View {
NavigationLink(destination: content) {
Text(title)
}
}
}

SwiftUI detect edit mode

It is on same level so environment is not visible, because it is activated for sub-views.

A possible solution is to separate dependent part into standalone view, like

    Form {
InternalView()
}
.toolbar {
EditButton()
}

Tested with Xcode 13.4 / iOS 15.5

demo

Test module on GitHub

SwiftUI Map causes modifying state during view update

Seems to me it's a bug in Map (as of Xcode Version 13.3.1 (13E500a) and iPhone 13 Simulator). If you switch to the Breakpoints side-bar and click the + and add an All Runtime Issues breakpoint, if you debug and click the button you'll hit the breakpoint and see this:

Sample Image

This trace shows that when the button is tapped to change the tracking state, SwiftUI updates MKMapView with the new state by calling _setUserTrackingMode (line 13) but a side effect of this is a callback to mapLayerDidChangeVisibleRegion (line 9) and it tries to set the value of a Binding (line 6), most likely the coordinateRegion. It shouldn't be setting a Binding while it is updating the MKMapView from the State, which is what results in the warning. We should all report the bug - I submitted it as FB9990674 under Developer Tools - SwiftUI, feel free to reference my number.



Related Topics



Leave a reply



Submit