What causes an 'AttributeGraph precondition failure' when using a TabView? - ios

I had a single view application wrapped in a NavigationView where I populate data fetched from an API. However, I needed to add a profile section with a TabView and sometimes when I run the application, right when the launch screen is displayed, I get the following error (about once every three times I run/ debug the app) and the app crashes.
AttributeGraph precondition failure: invalid value type for attribute: 56028 (expected PlatformItemList, saw BridgedTableViewState)
The view that causes the error:
TabView {
NavigationView {
MainView()
}
.tabItem { VStack {
Image(systemName: "list.dash")
Text("main")
}
}
NavigationView {
ProfileView()
}
.tabItem { VStack {
Image(systemName: "person.crop.circle")
Text("profile")
}
}
}
I removed the TabView and the profile view and the app runs without any exceptions consistently, which is why I thought it was an issue with the TabView. I searched for documentation on AttributeGraph, PlatformItemList and BridgedTableViewState but found none.

I had a related error: AttributeGraph precondition failure: invalid value type for attribute: 431640 (saw PreferenceKeys, expected ViewList).
This error did not arise in an app with almost identical functional and database code but using UIKit and manually-constructed tab view, list view etc. Only in the SwiftUI version with SwiftUI structs for these views.
Removing the Persistence class and persistence management seemed to resolve the issue (I use a direct SQLite coding for persistence instead of CoreData).
The Xcode error messages could be more helpful.

I had experienced same problem. It was caused by combination of AnyView and NSViewRepresentable wrapper for WKWebView. Getting rid of AnyView by rewriting helper function with #ViewBuilder fixed the problem.
Problem:
func viewFunc() -> AnyView {
switch self {
case .case1: return AnyView(ListOfWebViewWrappers())
default: return AnyView(WaitView())
}
}
Solution:
#ViewBuilder
func viewFunc() -> some View {
switch self {
case .case1: ListOfWebViewWrappers()
default: WaitView()
}
}
the #ViewBuilder allows SwiftUI to properly track each view's identity and properly manage resources. More information about view's identity: https://developer.apple.com/videos/play/wwdc2021/10022/

I spent a lot of time dealing with precondition failure crashes in an app with multiple tab views. Is there any chance that one or more of your specific views (ProfileView or MainView) is also using a NavigationView? If so, that's probably the problem. Xcode will happily let you add NavigationViews in each new view, but it's not actually the way SwiftUI is meant to work. Since each TabView is wrapped in a NavView, then you shouldn't need/use a NavView inside of those views.
(Also, probably just cosmetic, but in the code snippet above there seems to be an extra close curly brace before your second Nav view.)

Related

SwiftUI - Value of type 'some View' has no member 'resizable'

I'm new to SwiftUI coding and trying to learn step by step.
Sometimes I don't get what XCode is trying to tell me with its errors.
Following a super simple tutorial on how to resize an image.
Did everything ok until this error came up:
Value of type 'some View' has no member 'resizable'
I'm trying to resize an image in my assets folder.
This is my code:
import SwiftUI
struct ContentView: View {
...
...
var body: some View {
...
Image("home_illustration")
.padding(.bottom,60)
.resizable()
...
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
you have to use .resizable() above the .padding and below the Image. The reason is Image with padding is create a new child view so we can't apply .resizable() to view. It is for Image only. Let me know if you are not clear with my answer.
Each SwiftUI modifier returns a type. The final type of a View's body can be extremely complex, hence the existence of some View - that basically tells the compiler that you're returning something that conforms to the View protocol, but that you don't have the time or the energy to spell out exactly what it is, and anyway that's your job, you're a compiler, just figure it out.
Some modifiers only make sense when applied to certain types of view. In your case, .padding can be added to anything, so it's a method on the View protocol. It returns a type of ModifiedContent<SwiftUI.Image, SwiftUI._PaddingLayout>. These types build up the more modifiers you apply.
.resizable() is a method on Image, which returns an Image.
So if you resize, then pad the image, you're OK. If you apply padding, you aren't dealing with an image any more, so the compiler can't figure out what is happening.
Found the solution randomly by playing with my code.
The problem was the order of the modifiers.
"resizable()" should be always first.
Don't ask me why but...it just works.
Hopefully, this will help others.

How to provide a ‘System’ appearance choice in SwiftUI?

Main Question
Sorry for the long title. I’m trying to figure out how to provide a choice to user’s for setting the ColorScheme of the app to ‘System’.
As in, have the app default to the system’s dark mode / light mode. I know UIKit has a way to set the style to .unspecified but I’m not sure how to access that in SwiftUI.
I already tried setting .preferredColor() to nil and it kind of sort of works but sometimes it doesn’t. I’m new to managing state in Swift so I’m definitely doing something wrong.
Side Question:
When I set .preferredColor() to say .dark - the modal settings sheet I have doesn’t update? It always stays the system colour. I have .preferredColor() triggering on the ContentView() in the ...App.swift file.
Update
I solved my Side Question by just adding .preferredColor() to the modal sheet as well. It's not ideal but it does work. However I'm still unable to figure out how to set a .system preference in SwiftUI
I ended up finding two solutions to this problem. The first was found by jinjie and the other by me. Both solutions can be viewed here - repo. created by rizwankce.
Solution
What I ended up doing was using the package Introspect to grab the UIViewController off the main view I have using a ViewModifier.
Within the ViewModifier I just used .introspectViewController and that gave me the UIViewController so that I could then set UIViewController.overrideUserInterfaceStyle = .unspecified like this. Then I just did this conditionally based on a Picker not seen below. ⤵
/// All themes
enum Themes: String {
case Dark
case Light
case System
}
// MARK: - Theme Switch
struct ThemeSwitch: ViewModifier {
let appStorage: String
func body(content: Content) -> some View {
content
.introspectViewController { UIViewController in
switch appStorage {
case Themes.System.rawValue: UIViewController.overrideUserInterfaceStyle = .unspecified
case Themes.Dark.rawValue: UIViewController.overrideUserInterfaceStyle = .dark
case Themes.Light.rawValue: UIViewController.overrideUserInterfaceStyle = .light
default: UIViewController.overrideUserInterfaceStyle = .unspecified
}
}
}
}
// MARK: - Extensions
extension View {
func themeSwitch(appStorage: String) -> some View {
modifier(ThemeSwitch(appStorage: appStorage))
}
}
I just stored the desired theme as a String into a #State object which also updated an #AppStorage object so the app always had the selection no matter what.
You don't have to use Introspect and could instead create your own wrapper but I already had this package installed so I figured why not.
Alternative Solutions
The other solution is to make a view modifier and display the modified view conditionally. The condition is based on an optional array, dictionary, etc. of type ColorScheme. It either unwraps the optional if it's not nil or it just shows the unmodified view. You can find it at the repo. mentioned above under closed issues or pull requests.
In the future hopefully Apple will provide a way to do this natively but for now this will do! I'll try to update this answer in the future if something better comes along.

SwiftUI: Change only one instance of UITableViewAppearance

I know that the component List has an UITableView behind. I also know I can change the background by changing the UITableView.appearance().backgroundColor.
What I want is to change this for a single View that has a List component, without affecting the rest of the application. Is this possible? If so, how?
Thanks!
This is what I know needs to be done to achieve this:
var body: some View {
List {
//Your content
}
.onAppear {
//The specific table view settings.
//UITableView.appearance().separatorColor = .black
}
.onDisappear {
//The general table view settings
//UITableView.appearance().separatorColor = nil
}
}
But it is problematic for several reasons.
The biggest reason is the fact you might have 2 table views in the same View and you want them to have different appearances.
The second issue is that not all cases will call onAppear (I don't remember specific cases but I remember having issues with TabViews)
I hope there's already a better solution waiting in the next version of SwiftUI.

SwiftUI load data on push view OR reset on pop view

Swift 5.2
I have a NavigationView, for a tree of Views joined by NavigationLinks. (No weirdness going on; no cycles or hopping between different parts of the tree - just views linking to subviews linking to subsubviews.) One of them is a configuration screen. I desire that when you arrive on the config screen (aka ConfigView), the configuration is loaded from disk. In subviews of ConfigView, you can modify different settings, and on ConfigView you can save the settings. However, I desire that if you pop ConfigView (leave, towards the root of the tree), your unsaved changes are discarded.
One of my initial thoughts was to load the changes in the ConfigView initializer. This doesn't work, because ConfigView() is called in the parent view, so exiting the ConfigView and returning to ConfigView keeps the same ConfigView between views (and thus the same dirty data). (...Or, the ConfigView is recreated multiple times without actually leaving it, causing the data to be erroneously reloaded. I tried to test the conditions under which it did either, just now, but ran into a crash I'm not going to fix today. Regardless, I tested it earlier today and it demonstrated one or both of the above behaviors.)
Perhaps load it in the body block? But that gets re-run e.g. when you return from a subview link, which would erroneously reload the data.
Perhaps load it in .onAppear? But that ALSO erroneously reloads the data when you return from a subview.
I considered perhaps going with the dual of my original intent, disposing of the dirty data when you pop the ConfigView (and reloading it whenever requested), but I haven't found any hooks I can use to be notified when e.g. the user hits the "back" button.
How do I get a clean copy of the data to be present on new entry to the ConfigView, and not lost until the ConfigView is popped?
You should have a persistence data somewhere in your app, and when passing data to a child view create a draft data (ObservableObject). Then, save the updated data to the persistence data whenever needed. This way, the child will only play with the draft data and your parent view will only show the persistence data.
There's a good example here: https://developer.apple.com/tutorials/swiftui/working-with-ui-controls
Please have a look at all of the tutorials made by Apple, they are very short and powerful samples.
I will paste the code from the link above to here just in case the code is lost sometime in the future.
struct ProfileHost: View {
#Environment(\.editMode) var mode
#EnvironmentObject var userData: UserData
#State var draftProfile = Profile.default
var body: some View {
VStack(alignment: .leading, spacing: 20) {
HStack {
if self.mode?.wrappedValue == .active {
Button("Cancel") {
self.draftProfile = self.userData.profile
self.mode?.animation().wrappedValue = .inactive
}
}
Spacer()
EditButton()
}
if self.mode?.wrappedValue == .inactive {
ProfileSummary(profile: userData.profile)
} else {
ProfileEditor(profile: $draftProfile)
.onAppear {
self.draftProfile = self.userData.profile
}
.onDisappear {
self.userData.profile = self.draftProfile
}
}
}
.padding()
}
}

How update a SwiftUI List without animation

I want to update a SwiftUI List without any insert animation.
My List is getting its data from an #EnvironmentObject
I already tried to wrap the List itself and the PassthroughSubject.send() in a withAnimation(.empty) block but this does not help.
A very very dirty workaround is to call UIView.setAnimationsEnabled(false) (yes, UIKit has impact on SwiftUI), but there must be a SwiftUI-like way to set custom insert animations.
While the answer provided by DogCoffee works, it does so in an inefficient manner. Sometimes we do have to force the system to do what we want by being inefficient. In the case of implicit animations in SwiftUI, there is a better way to disable them.
Using the Transaction mechanism in SwiftUI, we can define an extension that can be applied to any view. This will disable animations for the view and any children.
For the list view example, this avoids replacing all the data in the list with a new, but identical copies.
extension View {
func animationsDisabled() -> some View {
return self.transaction { (tx: inout Transaction) in
tx.disablesAnimations = true
tx.animation = nil
}.animation(nil)
}
}
Try applying this extension to your list, or the parent containing view. You may have to experiment to find which view is ideal.
List {
// for each etc
}.animationsDisabled()
This works, just place .id(UUID()) at the end of your list
List {
// for each etc
}.id(UUID())
Sort of like reloadData for UIKit
on tvOS this works for me:
List {
...
}
.animation(.none)
.animate(nil)
you can find more info on https://developer.apple.com/tutorials/swiftui/animating-views-and-transitions

Resources