Post

Replies

Boosts

Views

Activity

How to change the background color of the status bar in SwiftUI when creating a custom NavigationController View
I wanted to experiment a little using UINavigationController with SwiftUI App Lifecycle I created a Custom Navigation Controller named JDNavigationController. It does not do much other than having the @Observable macro public struct NavigationControllerStack<Content: View>: UIViewControllerRepresentable { private let initialView: () -> Content @State private var controller = JDNavigationController() public init(content: @escaping () -> Content) { self.initialView = content } public func makeUIViewController(context: Context) -> JDNavigationController { let viewController = self.initialView() .environment(self.controller) .viewController self.controller.viewControllers = [ viewController ] return controller } public func updateUIViewController(_ uiViewController: JDNavigationController, context: Context) { } public typealias UIViewControllerType = JDNavigationController } when I check, the functionality of the navigation controller works as expected, however, I can't for the life of me to change the background color of the status bar. #Preview { NavigationControllerStack { ZStack { Color.purple .ignoresSafeArea(.all) VStack { Text("somethign") } } } } I have tried Using the appearance functions Creating a Custom View Controller, and in that view controller I tried using a Hosting Controller by adding the hosting controller as a child and setting hostingController.view.insetsLayoutMarginsFromSafeArea = false using the toolbar modifiers from SwiftUI View none seems to work. I opened the View Debugger it looks like the white portion comes from the UIHosting Controller but I am not sure what I can do to fix it
6
0
1.8k
Jul ’24
Relationship Fault not happening in SwiftData
I have two models a Person and a Possession the Person model has a one to many relationship to the Possession model. meaning each possession can only have one person but a person can have multiple possessions. I have set my model like the following Person: @Model class Person { @Attribute(.unique) let personID: String @Relationship(.cascade, inverse: \Possession.person) var possetions: [Possession]? init(id: String, possessions: [Possession]) { self.personID = id self.possetions = possessions } } Possession: @Model class Possession { @Attribute(.unique) let id: String let name: String? var person: Person? init(id: String, name: String, person: Person) { self.id = id self.name = name self.person = person } } If i set a breakpoint i see that all the posessions are loaded into the memory this is something i do not want to happen. In Core Data we get a relationship fault however, i am not seeing the same behavior in SwiftData. here's how my view is implemented struct ContentView: View { @Environment(\.modelContext) private var modelContext @EnvironmentObject private var navigationStore: NavigationStore @Query() private var people: [Person] var body: some View { List { ForEach(people) { person in NavigationLink(value: person) { VStack { Text(person.personID) } } .swipeActions { Button("Delete") { modelContext.delete(person) } } } } .toolbar(content: { Button("Add") { let newPErson = Person(id: UUID().uuidString, possessions: []) modelContext.insert(newPErson) do { try modelContext.save() } catch { assertionFailure("\(error)") } } }) .navigationDestination(for: Person.self) { person in Text("hello") } } } at the launch i do not want posessions to be loaded into the memory. I want them loaded when they are being used.
0
0
930
Jul ’23
SwiftData how to release the relationship data after popping the view back
ViewA has ModelA ViewA loads into the memory. ModelA is queried and shown in the list. ModelA has an array of ModelB which is faulted for the memory. When you select an item in the list in ViewA ViewB is loaded and ModelB information is displayed. When you pop back to ViewA, ModelB is still in the memory since it is contained within the array of ModelA. how could i make this memory efficient by releasing ModelB from the memory. should I refetch everything? or is there another method?
1
0
508
Feb ’24
How to change the background color of the status bar in SwiftUI when creating a custom NavigationController View
I wanted to experiment a little using UINavigationController with SwiftUI App Lifecycle I created a Custom Navigation Controller named JDNavigationController. It does not do much other than having the @Observable macro public struct NavigationControllerStack<Content: View>: UIViewControllerRepresentable { private let initialView: () -> Content @State private var controller = JDNavigationController() public init(content: @escaping () -> Content) { self.initialView = content } public func makeUIViewController(context: Context) -> JDNavigationController { let viewController = self.initialView() .environment(self.controller) .viewController self.controller.viewControllers = [ viewController ] return controller } public func updateUIViewController(_ uiViewController: JDNavigationController, context: Context) { } public typealias UIViewControllerType = JDNavigationController } when I check, the functionality of the navigation controller works as expected, however, I can't for the life of me to change the background color of the status bar. #Preview { NavigationControllerStack { ZStack { Color.purple .ignoresSafeArea(.all) VStack { Text("somethign") } } } } I have tried Using the appearance functions Creating a Custom View Controller, and in that view controller I tried using a Hosting Controller by adding the hosting controller as a child and setting hostingController.view.insetsLayoutMarginsFromSafeArea = false using the toolbar modifiers from SwiftUI View none seems to work. I opened the View Debugger it looks like the white portion comes from the UIHosting Controller but I am not sure what I can do to fix it
Replies
6
Boosts
0
Views
1.8k
Activity
Jul ’24
Relationship Fault not happening in SwiftData
I have two models a Person and a Possession the Person model has a one to many relationship to the Possession model. meaning each possession can only have one person but a person can have multiple possessions. I have set my model like the following Person: @Model class Person { @Attribute(.unique) let personID: String @Relationship(.cascade, inverse: \Possession.person) var possetions: [Possession]? init(id: String, possessions: [Possession]) { self.personID = id self.possetions = possessions } } Possession: @Model class Possession { @Attribute(.unique) let id: String let name: String? var person: Person? init(id: String, name: String, person: Person) { self.id = id self.name = name self.person = person } } If i set a breakpoint i see that all the posessions are loaded into the memory this is something i do not want to happen. In Core Data we get a relationship fault however, i am not seeing the same behavior in SwiftData. here's how my view is implemented struct ContentView: View { @Environment(\.modelContext) private var modelContext @EnvironmentObject private var navigationStore: NavigationStore @Query() private var people: [Person] var body: some View { List { ForEach(people) { person in NavigationLink(value: person) { VStack { Text(person.personID) } } .swipeActions { Button("Delete") { modelContext.delete(person) } } } } .toolbar(content: { Button("Add") { let newPErson = Person(id: UUID().uuidString, possessions: []) modelContext.insert(newPErson) do { try modelContext.save() } catch { assertionFailure("\(error)") } } }) .navigationDestination(for: Person.self) { person in Text("hello") } } } at the launch i do not want posessions to be loaded into the memory. I want them loaded when they are being used.
Replies
0
Boosts
0
Views
930
Activity
Jul ’23
SwiftData how to release the relationship data after popping the view back
ViewA has ModelA ViewA loads into the memory. ModelA is queried and shown in the list. ModelA has an array of ModelB which is faulted for the memory. When you select an item in the list in ViewA ViewB is loaded and ModelB information is displayed. When you pop back to ViewA, ModelB is still in the memory since it is contained within the array of ModelA. how could i make this memory efficient by releasing ModelB from the memory. should I refetch everything? or is there another method?
Replies
1
Boosts
0
Views
508
Activity
Feb ’24